سؤال

I want to ask, if is there some way, how to set different menu icons in ActionBar in different tabs. 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.

Is there some way, how to do that? (I am using ActionBar Fragment Navigaton Tabs)

Thanks for any advice

هل كانت مفيدة؟

المحلول

Call invalidateOptionsMenu() in onTabSelected method, which would call back onPrepareOptionsMenu . You can write menu hiding logic inside onPrepareOptionsMenu as like following.

/**
     * 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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top