【问题标题】:How to add progressbar to ActionBarSherlock如何将进度条添加到 ActionBarSherlock
【发布时间】:2012-05-25 13:24:35
【问题描述】:

是否可以在 ActionBarSherlock 中添加进度条? 我需要在特定时间显示和隐藏它。但它必须位于 ActionBarSherlock 内。

我班级的代码。你可以看到,我使用 requestWindowFeature(Window.FEATURE_PROGRESS) 但它没有任何区别。同样的结果。 :

public class RecipeActivity extends SherlockActivity {

private String picture;
private String TAG = "RecipeActivity";
private ImageView viewPicture;
private RecipeGeneral recipeGeneral;
private Recipe recipe;
private String [] pictures;
private ArrayList<Step> steps;
//private ImageView viewStar;
private DataBaseFactory db;
private NyamApplication application;
private TextView viewDescription;
private TextView userView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);

    try {
        setContentView(R.layout.recipe_page);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        recipeGeneral = (RecipeGeneral)getIntent().getSerializableExtra("Recipe");
        Log.d(TAG, "recipeGeneral = " + recipeGeneral.toString());
        picture = Constants.URL + recipeGeneral.getImg_url();
        Log.d(TAG,"picture : " + picture);
        viewPicture = (ImageView)findViewById(R.id.picture);
        new DownloadImageTask().execute(pictures);

        viewDescription = (TextView)findViewById(R.id.recepy_description);
        TextView viewTitle = (TextView)findViewById(R.id.recepy_title);
        TextView ratingView = (TextView)findViewById(R.id.txtTitle3);
        userView = (TextView)findViewById(R.id.author_name);

        application = (NyamApplication)getApplication();
        db = application.getDB();

        if (getIntent().getBooleanExtra("isFavorites", false) == true) {
            Log.d(TAG, "isFavorites = " + getIntent().getBooleanExtra("isFavorites", false));

            viewDescription.setText(((Recipe)recipeGeneral).getDescription());
            viewTitle.setText(recipeGeneral.getTitle());
            Log.d(TAG, "Title = " + recipeGeneral.getTitle());
            ratingView.setText(String.valueOf(recipeGeneral.getFavorites_by()));
            Log.d(TAG, "Rating = " + String.valueOf(recipeGeneral.getFavorites_by()));
            userView.setText(((Recipe)recipeGeneral).getUser());
            Log.d(TAG, "User = " + ((Recipe)recipeGeneral).getUser());


            steps = db.getStepsByRecipeId(recipeGeneral.getId());
            if (steps != null) {
                ((Recipe)recipeGeneral).setSteps(steps);
            }

        } else {
            Log.d(TAG, "isFavorites = " + getIntent().getBooleanExtra("isFavorites", false));
            Object [] params = new Object[] {this, Constants.URL + recipeGeneral.getPath()+ Constants.JSON};

            new AsyncHttpGetRecipe().execute(params);   
            new AsyncHttpGetSteps().execute(params);    
            viewTitle.setText(recipeGeneral.getTitle());

            ratingView.setText(recipeGeneral.getFavorites_by());
            Log.d(TAG, "Rating = " + recipeGeneral.getFavorites_by());

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}


private class DownloadImageTask extends AsyncTask<String,Void,Bitmap> {

    Bitmap  bitmap = null;

    @Override
    protected Bitmap doInBackground(String... str) {
        try{   
            InputStream in = new java.net.URL(picture).openStream();
            bitmap = BitmapFactory.decodeStream(new SanInputStream(in));
            //viewPicture.setImageBitmap(bitmap);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override 
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        viewPicture.setBackgroundDrawable(new BitmapDrawable(bitmap));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.item_page_menu, (com.actionbarsherlock.view.Menu) menu);
    return super.onCreateOptionsMenu(menu);
}

public void onClick(View v) {
    switch(v.getId()) {
    case R.id.icon_little3:
        Log.d(TAG,"further");
        if (getIntent().getBooleanExtra("isFavorites", false) == false) {
            if (recipe != null && steps != null) {
                recipe.setSteps(steps);
                db.addRecipeToFavorites(recipe);
            }
        }
    }
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())  {
    case R.id.menu_further: 
        Log.d(TAG,"In menu_further");
        Intent stepIntent = new Intent(this, StepActivity1.class);

        if (getIntent().getBooleanExtra("isFavorites", false) == true) {
            Log.d(TAG,"id: " + recipeGeneral.getId());
            Log.d(TAG,"desc: " + recipeGeneral.getTitle());
            stepIntent.putExtra("Recipe1", recipeGeneral);
        } else {
            Log.d(TAG,"id: " + recipe.getId());
            Log.d(TAG,"desc: " + recipe.getTitle());
            recipe.setSteps(steps);
            stepIntent.putExtra("Recipe1", recipe);
        }
        startActivity(stepIntent);
        return true;
    case R.id.menu_cart: 
        Toast.makeText(this, "Cart", 10000).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

}


class AsyncHttpGetRecipe extends AsyncTask<Object, String, Recipe> {

    @Override
    protected  Recipe doInBackground(Object... params) {
        Recipe recipeAsync = null;
        try {
            recipeAsync = ApiFactory.getRecipe((Context)params[0], (String)params[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        } 
        recipe  = recipeAsync;
        return recipeAsync;
    }

    @Override
    protected void onPostExecute(Recipe recipeTemp) {
        viewDescription.setText(recipeTemp.getDescription());
        Log.d(TAG, "Description = " + recipeTemp.getDescription());
        userView.setText(recipeTemp.getUser());
        Log.d(TAG, "User = " + recipeTemp.getUser());

    }
}

class AsyncHttpGetSteps extends AsyncTask<Object, String, ArrayList<Step>> {

    @Override
    protected  ArrayList<Step> doInBackground(Object... params) {
        ArrayList<Step> stepsAsync = null;
        try {
            stepsAsync = ApiFactory.getSteps((Context)params[0],  (String)params[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return stepsAsync;
    }

    @Override
    protected void onPostExecute(ArrayList<Step> stepsAsync) {
        steps = stepsAsync;
    }
}

}

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:

    是的。您可以使用 ABS 将 ProgressBar 添加到 ActionBar。

    这是下面提供的来源的摘录,如果解决方案有帮助,请 +1 原始海报;-)

    在您的onCreate() 方法中,添加这段代码:

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
    setProgressBarIndeterminateVisibility(true);
    

    当您完成需要显示 ProgressBar 的任务后,使用以下代码隐藏 ProgressBar:

    setProgressBarIndeterminateVisibility(false);
    

    信用:https://stackoverflow.com/a/9157874/450534

    【讨论】:

    • 没关系t show progressbar or any aditional animation. This two lines just disable my actionbar from work. Now it throws exception after pushing any button from actionbar. It doesnt,我只需要进度条。我做错了什么?
    • 你正在调用Window.FEATURE_PROGRESS
    • @Radu:我看到您也针对同一问题发布了一个问题。但我害怕,我从未见过这种行为。让我看看我是否可以使用问题中的代码并重现行为。
    • @SiddharthLele 你好,你可以使用 ActionBarSherlock 的示例演示吗?不需要使用我的代码,ABS 演示做同样的事情。我在 ABS 演示中唯一更改的是 support-v4.jar 版本,以匹配最新版本。
    【解决方案2】:

    是的,你可以,ABS 有这样的功能。下一段代码来自 ABS 的 Demos 示例应用:

    public class Progress extends SherlockActivity  {
        Handler mHandler = new Handler();
        Runnable mProgressRunner = new Runnable() {
            @Override
            public void run() {
                mProgress += 2;
    
                //Normalize our progress along the progress bar's scale
                int progress = (Window.PROGRESS_END - Window.PROGRESS_START) / 100 * mProgress;
                setSupportProgress(progress);
    
                if (mProgress < 100) {
                    mHandler.postDelayed(mProgressRunner, 50);
                }
            }
        };
    
        private int mProgress = 100;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setTheme(SampleList.THEME); //Used for theme switching in samples
            super.onCreate(savedInstanceState);
    
            //This has to be called before setContentView and you must use the
            //class in com.actionbarsherlock.view and NOT android.view
            requestWindowFeature(Window.FEATURE_PROGRESS);
    
            setContentView(R.layout.progress);
    
            findViewById(R.id.go).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (mProgress == 100) {
                        mProgress = 0;
                        mProgressRunner.run();
                    }
                }
            });
        }
    }
    

    这个示例可以在 ABS 的 samples\demos\ 目录中找到(Eclipse 工作空间中的 Progress.java)

    【讨论】:

    • 尝试 setSupportProgressBarIndeterminateVisibility(true);而是 setProgressBarIndeterminateVisibility(true);和 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);而是 requestWindowFeature(Window.FEATURE_PROGRESS);
    • 如果你想要 IndeterminateProgress
    • 我认为它总是首先出现在操作栏菜单中,但我不确定。再问一个问题,也许有人知道
    【解决方案3】:

    为了完全兼容,您应该使用:

    setSupportProgressBarIndeterminateVisibility(true);
    

    这是一个例子:

    public class MyActivity extends SherlockFragmentActivity {
        //...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);     
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        
            setContentView(R.layout.my_layout);
            //...
            setSupportProgressBarIndeterminateVisibility(true);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多