1

很明显,要显示/隐藏不确定的进度,您必须使用此方法:

itemMenu.setActionView(...);

所以:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_refresh:
            item.setActionView(R.layout.indeterminate_progress_action);
            runAsyncTask();
    ...
    }
}

我不清楚的是:如何将动作视图设置回 null。我不想保留对 MenuItem 的引用,我认为这是不正确的,因为我不想假设有关 OptionsMenu 生命周期的任何事情。

我应该如何在 onPostExecute 上将操作视图设置为空?

4

5 回答 5

5

我这样做:

refresh = false; // the future state of your indeterminate progress
invalidateOptionsMenu(); // trigger the recreation of the menu and call onCreateOptionsMenu(Menu)

然后在您的 onCreateOptionsMenu(Menu) 方法中:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu, inflater);

    MenuItem refreshItem = menu.add(Menu.NONE, R.id.action_bar_refresh, 1, "Refresh");
    refreshItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    if(refresh)
        refreshItem.setActionView(R.layout.indeterminate_progress_action);
    else
        refreshItem.setIcon(R.drawable.ic_menu_refresh);
}
于 2012-09-06T18:59:42.250 回答
0

怎么样invalidateOptionsMenu()?那么,您onCreateOptionsMenu()可能总是将其设置为null

于 2012-09-06T18:15:17.310 回答
0

我相信你可以调用setActionView(null)那个 MenuItem

然后刷新 ActionBarinvalidateOptionsMenu()

于 2012-09-06T18:16:07.727 回答
0

如果您不想这样做,则无需操作特定的菜单项。

I use a different approach that leverages off the system's indeterminate progress bar functionality (as ported to ActionBarSherlock). I explain it here to offer more options to future readers to use whichever way works best for them.


My base fragment has a method I call to turn my loading UI on & off. Here is a trimmed down version:

private void setLoading(final boolean isLoading)
{
    final SherlockFragmentActivity sherlockActivity = getSherlockActivity();
    sherlockActivity.runOnUiThread(new Runnable()
    {
        public void run()
        {
            // show loading progress bar
            sherlockActivity
                .setSupportProgressBarIndeterminateVisibility(isLoading);

            // hide the rest of the menu
            setMenuVisibility(!isLoading);
        }
    });
}

For this to work, your activity must be configured to use the correct style - call this from your SherlockFragmentActivity.onCreate() method:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

The final trick is that on pre-HoneyComb devices, this causes the progress bar to show immediately by default (instead of being hidden by default from HC & above).

  • you must set it to be invisible
  • you must also create a Sherlock Action Bar instance before this code will work

The onCreate() thus becomes:

protected void onCreate(Bundle arg0)
{
    super.onCreate(arg0);

    // allow window to show progress spinner in the action bar
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    getSupportActionBar();
    setSupportProgressBarIndeterminateVisibility(false); 
}

For more details on this you can check my answer to

于 2012-11-06T09:16:29.553 回答
0

If you don't want to keep a reference to the menu item, then you can simply keep a reference to the Menu object instead. A reference to the Menu is guaranteed to remain valid until the next time onCreateOptionsMenu() is called as mentioned in the docs (http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)).

Then call:

MenuItem menuItem = mMenu.findItem(R.id.favourite_payment);
menuItem.setActionView(null);

Or alternatively if using AppCompat:

MenuItem menuItem = mMenu.findItem(R.id.favourite_payment);
MenuItemCompat.setActionView(menuItem, null);

There is no need to call invalidateOptionsMenu().

于 2014-07-01T22:53:19.473 回答