简体   繁体   English

在每个标签中更改ActionBar图标

[英]Change ActionBar icons in each tab

I want to ask, if is there some way, how to set different menu icons in ActionBar in different tabs . 我想问一下,是否有办法在ActionBar 的不同选项卡中设置不同的菜单图标 For example, if the user is in tab number one, in the ActionBar would appear search icon and if the user is in tab number one, there would appear refresh icon. 例如,如果用户位于第一选项卡中,则在ActionBar中将显示搜索图标,如果用户位于第一选项卡中,则将显示刷新图标。

Is there some way, how to do that? 有什么办法,怎么做? (I am using ActionBar Fragment Navigaton Tabs ) (我正在使用ActionBar Fragment Navigaton Tabs

Thanks for any advice 感谢您的任何建议

Call invalidateOptionsMenu() in onTabSelected method, which would call back onPrepareOptionsMenu . onTabSelected方法中调用invalidateOptionsMenu() ,这将回调onPrepareOptionsMenu You can write menu hiding logic inside onPrepareOptionsMenu as like following. 您可以在onPrepareOptionsMenu内编写菜单隐藏逻辑,如下所示。

/**
     * Called whenever we call invalidateOptionsMenu()
     */
    @Override
    public void onPrepareOptionsMenu(Menu menu) {

        super.onPrepareOptionsMenu(menu);

        try {
            if(tab 1 selected){
            menu.findItem(R.id.menu_search).setVisible(true);
            menu.findItem(R.id.menu_refresh).setVisible(false);
                }
                else if(tab 2 selected){
                    menu.findItem(R.id.menu_search).setVisible(false);
            menu.findItem(R.id.menu_refresh).setVisible(true);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM