如何更新 ActionBar 中显示的菜单项?

我有一个活动有2个片段。它们都是 ListFragments,都将 MenuItems 贡献给 Menu。我有一个 MenuItem,我已经设置了属性 android: showAsAction,让它在 ActionBar 上显示为一个按钮。效果不错。

现在 MenuItem 依赖于状态。它是暂停/恢复菜单选项,用于暂停和恢复队列。我的问题是我不知道如何设置它的初始雕像当碎片被创建。

它的状态取决于队列是否暂停。因此,我有一个 AsyncTask,它获取队列并根据队列的状态设置布尔值(暂停)。我调用 onPrepareOptionsMenu 根据队列的最后一个已知状态来设置“暂停”菜单项的文本,如果我将 MenuItem 保留在实际菜单中,那么这个工作得非常好。您点击菜单图标,onPrepareOptionsMenu 被激发,并且菜单在显示之前被更新。

问题是,如果我将相同的 MenuItem 放在 ActionBar (showAsAction)上,如何在不调用 PrepareOptionsMenu 的情况下强制它更新?我需要能够做到这一点,因为在第一次启动应用程序时,我发送了一个请求来获取队列,但任务在设置和显示 ActionBar 之后返回。我已经在片段中创建了一个处理程序,每次获得队列更新时都会调用它,但是从那里开始,我如何更新 ActionBar 上 MenuItem 的文本呢?我似乎无法找到一种方法来获得目前设置菜单操作它,除了在准备选项菜单。

Rob W.

107548 次浏览

Option #1: Try invalidateOptionsMenu(). I don't know if this will force an immediate redraw of the action bar or not.

Option #2: See if getActionView() returns anything for the affected MenuItem. It is possible that showAsAction simply automatically creates action views for you. If so, you can presumably enable/disable that View.

I can't seem to find a way to get the currently set Menu to manipulate it except for in onPrepareOptionMenu.

You can hang onto the Menu object you were handed in onCreateOptionsMenu(). Quoting the docs:

You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.

in my case: invalidateOptionsMenu just re-setted the text to the original one, but directly accessing the menu item and re-writing the desire text worked without problems:

if (mnuTopMenuActionBar_ != null) {
MenuItem mnuPageIndex = mnuTopMenuActionBar_
.findItem(R.id.menu_magazin_pageOfPage_text);


if (mnuPageIndex != null) {
if (getScreenOrientation() == 1) {
mnuPageIndex.setTitle((i + 1) + " von " + pages);
}
else {
mnuPageIndex.setTitle(
(i + 1) + " + " + (i + 2) + " " + " von " + pages);
}
// invalidateOptionsMenu();
}
}

due to the comment below, I was able to access the menu item via the following code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.magazine_swipe_activity, menu);
mnuTopMenuActionBar_ = menu;
return true;
}

I have used this code:

public boolean onPrepareOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
TextView title  = (TextView) findViewById(R.id.title);
menu.getItem(0).setTitle(
getString(R.string.payFor) + " " + title.getText().toString());
menu.getItem(1).setTitle(getString(R.string.payFor) + "...");
return true;
}

And worked like a charm to me you can find OnPrepareOptionsMenu here

For clarity, I thought that a direct example of grabbing onto a resource can be shown from the following that I think contributes to the response for this question with a quick direct example.

private MenuItem menuItem_;


@Override
public boolean onCreateOptionsMenu(Menu menuF)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_layout, menuF);
menuItem_ = menuF.findItem(R.id.menu_item_identifier);
return true;
}

In this case you hold onto a MenuItem reference at the beginning and then change the state of it (for icon state changes for example) at a later given point in time.

First please follow the two lines of codes to update the action bar items before that you should set a condition in oncreateOptionMenu(). For example:

Boolean mISQuizItemSelected = false;


/**
* Called to inflate the action bar menus
*
* @param menu
*      the menu
*
* @return true, if successful
*/


@Override
public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu items for use in the action bar


inflater.inflate(R.menu.menu_demo, menu);


//condition to hide the menus
if (mISQuizItemSelected) {
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setVisible(false);
}
}


return super.onCreateOptionsMenu(menu);
}


/**
* Called when the item on the action bar being selected.
*
* @param item
*      menuitem being selected
*
* @return true if the menuitem id being selected is matched
* false if none of the menuitems id are matched
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getId() == R.id.action_quiz) {
//to navigate based on the usertype either learner or leo
mISQuizItemSelected = true;


ActionBar actionBar = getActionBar();
invalidateOptionMenu();
}
}

To refresh menu from Fragment simply call:

getActivity().invalidateOptionsMenu();