更改片段中菜单项的可见性 [英] Changing visibility of menu items in fragment

查看:91
本文介绍了更改片段中菜单项的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当片段更改时,我正在尝试隐藏一些菜单项,但似乎不起作用。我在做什么:
定义菜单和菜单项:

I'm trying to hide some menu items when a fragment is changed, but seems that this is not working. Here is what im doing: Defining the menu and menu items:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar_sharecall, menu);

        actionMenu = menu;
        MenuItem searchItem = menu.findItem(R.id.action_searchmenuitem);
        MenuItem item = menu.findItem(R.id.action_menushare);
        // item.setVisible(false);
        // searchItem.setVisible(false);

        topSearch = searchItem;
        topShare = item;
        final MRShareActionProvider actionProvider = new MRShareActionProvider(
                this);
        MenuItemCompat.setActionProvider(item, actionProvider);
        actionProvider
                .setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        actionProvider.setOnShareTargetSelectedListener(this);
        actionProvider.setShareIntent(createShareIntent());
        return true;

    }

更改片段并更改可见性:

changing the fragment and changing the visibility:

  //changing visibility    
    topSearch.setVisible(false);
    frag = new SyncFragment();
    FragmentTransaction ft = getSupportFragmentManager()
    .beginTransaction();
    ft.replace(R.id.fragment_content, frag);
    ft.commitAllowingStateLoss();

这是我的SyncFragment:

and this is my SyncFragment:

public class SyncFragment extends MRBaseACBFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sync, null);
    }


}

片段已更改,我仍然可以看到菜单项。有人可以帮我解决该问题的方法吗?

but after the fragment is changed, i can still see the menu item. Can someone help me with a solution on how to do this?

推荐答案

我不知道为什么它不起作用,但是尝试直接在 onCreateOptionMenu 中更改可见性。

I don't know why it does not work, but try to change the visibility directly in the onCreateOptionMenu.

如果可行,那么当您想隐藏它时,调用 invalidateOptionsMenu (或 supportInvalidateOptionsMenu 用于操作栏兼容性),它将强制 onCreateOptionMenu 再次调用,如果片段不存在,您可以更新菜单。

If it works, then when you want to hide it call invalidateOptionsMenu (or supportInvalidateOptionsMenu for actionbar compat), it will force the onCreateOptionMenu to be called again and you can update the menu if the fragment is there or not.

这篇关于更改片段中菜单项的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆