Changing options menu icon in actionbar depending on an open Fragment

不羁的心 提交于 2019-12-10 01:08:39

问题


I have this item in my options menu:

<item
    android:id="@+id/opt_mnu_action"
    android:icon="@android:drawable/ic_dialog_info"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="New">
</item>

The menu itself created in main FragmentActivity. I want to change this item's icon programmatically depending on the open Fragment and, obviously, have different actions when the user hits this button. I tried several things to do that, but nothing worked. The last thing I tried was this code in my Fragment's onCreateView method:

MenuItem mi = (MenuItem) view.findViewById(R.id.opt_mnu_action);
mi.setIcon(R.drawable.ico_1);

But my app crashed. So is there a way to do that?

**UPDATE**

Here's what I'm trying to do now, all in my main main FragmentActivity: First of all I have a MenuItem action_button; in my hierarchy view. Then in my onCreateOptionsMenu method I instantiate it:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    action_button = menu.findItem(R.id.opt_mnu_action);
    return super.onCreateOptionsMenu(menu);
}

Then I created this function to change the icon according to the open tab:

public void change_action_button_icon(int tab_position)
{
    switch(tab_position)
    {
    case 0:
        action_button.setIcon(R.drawable.ico_1);
        break;
    case 1:
        action_button.setIcon(R.drawable.ico_2);
        break;
    case 2:
        action_button.setIcon(R.drawable.ico_3);
        break;
    }
    invalidateOptionsMenu();
}

And I call it in my onTabSelected method:

public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
    setTab_position(tab.getPosition());
    change_action_button_icon(tab.getPosition());
}

But once I start my app - it crashes. I get NullPointerException error at this line:

action_button.setIcon(R.drawable.ico_1);

My guess - it happens because the icon change was requested before the action_button was instantiated. But I don't know how to overcome it...


回答1:


Use this to get a reference to the menu item:

    menu.findItem(resourceId).setIcon(drawableId);

You have to put the code to change the icon in onCreateOptionsMenu().

Please refer to my example below:

    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.option_menu, menu);

    if (needToChangeMenuItem){

        menu.findItem(resourceId).setIcon(drawableId);
    }

    manageMenuIcon(menu);

    needToChangeMenuItem = false;


    return true;
}


    public void manageMenuIcon(Menu menu){
    if (bluetoothIconOn){
        menu.findItem(R.id.secure_connect_scan).setIcon(R.drawable.bluetoothon);
    } else
        menu.findItem(R.id.secure_connect_scan).setIcon(R.drawable.bluetoothoff);

    if (gpsIconOn)
        menu.findItem(R.id.gps).setIcon(R.drawable.gps);
    else
        menu.findItem(R.id.gps).setVisible(false);

    if (slipAndDropIconOn)
        menu.findItem(R.id.fall).setIcon(R.drawable.fall);
    else
        menu.findItem(R.id.fall).setVisible(false);

    if (fesConnectIconOn)
        menu.findItem(R.id.fesConnection).setIcon(R.drawable.fesconnect);
    else
        menu.findItem(R.id.fesConnection).setVisible(false);


}


    public void changeMenuItem(int resId, int draId){


    needToChangeMenuItem = true;
    resourceId = resId;
    drawableId = draId;

    invalidateOptionsMenu();        


}



回答2:


MenuItem mi = (MenuItem) view.findViewById(R.id.opt_mnu_action);
mi.setIcon(R.drawable.ico_1);

In your Fragment's onCreateOptionsMenu, load this menu, and keep a reference to the menu item (it is not part of the fragment's view hierarchy so you can't use findViewById).

Whenerver you are ready to update the icon, use mi.setIcon(R.drawable.ico_1); and call invalidateOptionsMenu().

UPDATED:

return super.onCreateOptionsMenu(menu);

This will actually return false, since the base implementation doesn't do that. Instead skip it, or just call super.onCreateOptionsMenu(menu); first, do your stuff and then return true.




回答3:


First add actionOverFlowButtonStyle into your main theme

<style name="AppTheme" parent="AppBaseTheme">

    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

Define the New style for action over flow button

<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_menu</item>
</style>


来源:https://stackoverflow.com/questions/19184279/changing-options-menu-icon-in-actionbar-depending-on-an-open-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!