Hide MenuItem in some Fragments

后端 未结 13 2152
南方客
南方客 2020-12-07 20:00

I using menu drawer which has more Fragments. In some Fragments I have menu item REFRESH but in some fragments I want hide this menu item (I don\'t

13条回答
  •  死守一世寂寞
    2020-12-07 20:43

    There are many different versions of similar solutions but unfortunately, none of them worked for me. I am sharing what eventually was useful for me to hide the whole overflow menu with multiple menu items. Thought maybe it's useful for anyone.

    I grouped my menus with an id and then referred that id

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.setGroupVisible(R.id.menu_overflow, false);
        super.onPrepareOptionsMenu(menu);
    }
    

    If you want to hide any individual menu item then you can use

    menu.getItem(R.id.action_licenses).setVisible(false);
    

    Important thing is that you should have setOptionsMenu(true) in onViewCreated()

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
    

提交回复
热议问题