重复片段中的菜单和数据使用FragmentStatePagerAdapter后 [英] Duplicate Menu and Data in Fragments after using FragmentStatePagerAdapter

查看:194
本文介绍了重复片段中的菜单和数据使用FragmentStatePagerAdapter后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在这里各个岗位提供了这么多的答案,但没有为我工作。

问题 - 我有了6个片段,但单一的活动导航的抽屉里。一切正常,直到我在抽屉里改变排名第1片段。我想第一个片段内刷卡标签。所以我用 FragmentStatePagerAdapter


  • 每个片段都有自己的菜单与MainActivity菜单。

      @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);    //通知系统,使该片段的选项菜单。
        setHasOptionsMenu(真);
    }

    和膨胀是这样的:

      @覆盖
    公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
        inflater.inflate(R.menu.story,菜单);
    }


  • 一切工作正常。但是,当我在抽屉式导航访问其他片段则显示的工具栏重复菜单。它创造了更多的副本,如果有留在当我访问其他片段的工具栏空间。


尝试1 :要解决这个问题,我最初使用的:

  @覆盖
prepareOptionsMenu(菜单菜单)上公共无效{
    menu.clear();
}@覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
    super.onCreateOptionsMenu(菜单,充气器);
    inflater.inflate(R.menu.story,菜单);
}

有了这个,我没有得到重复的菜单,但现在我没有看到 MainActivity菜单

2试试

  @覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
    。getActivity()invalidateOptionsMenu();
    inflater.inflate(R.menu.story,菜单);
}

有了这个,我同时获得片段和活动菜单,但重复的存在。

这应该很容易解决,但我不捡的方式来处理这个问题。也许我不明白的生命周期呢?

我的另一种方法 - 实现所有菜单中的片段就可以了,但是这应该是我们最后的选择。

解决这个 - 既保持菜单上的所有我需要做的就是这个(很简单的解决方案):

  menu.clear();
inflater.inflate(R.menu.story,菜单);
。getActivity()getMenuInflater()膨胀(R.menu.main,菜单);

问题2 OnOptionsItemSelected 从第一个片段的方法获取调用其他片段。


解决方案

 私人无效hideAllMenuItems(){
        如果(actionBarMenu!= NULL){
            actionBarMenu.findItem(R.id.action_item1).setVisible(假);
            actionBarMenu.findItem(R.id.action_item2).setVisible(假);
        }
    }
    私人无效showMenuIcon(){
        如果(actionBarMenu!= NULL){
            hideAllMenuItems();
            如果(currentFragment的instanceof片段1)
                actionBarMenu.findItem(R.id.action_item1).setVisible(真);            否则,如果(currentFragment的instanceof Fragment2)
                actionBarMenu.findItem(R.id.action_item2).setVisible(真);        }
    }

通话shoeMenuIcon()每次有新的片段负荷..

希望你正在寻找这个

I tried so many answers provided by various posts here but nothing worked for me.

Problem- I have navigation drawer that has 6 fragments but single activity. Everything worked fine till I changed 1st ranked fragment in drawer. I wanted Swipe tabs inside first fragment. So I used FragmentStatePagerAdapter.

  • Each fragment has its own menu along with MainActivity Menu.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Notify the system to allow an options menu for this fragment.
        setHasOptionsMenu(true);
    }
    

    And inflated like this:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.story, menu);
    }
    

  • Everything works fine. But When I visit other fragments in navigation drawer then it shows duplicate menu in toolbar. It creates more duplicates if there is space left in toolbar when I visit other fragments.

Try 1 : To solve this problem I initially used:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.clear();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.story, menu);
}

With this I don't get duplicate menu but now I don't see MainActivity menus.

Try 2:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    getActivity().invalidateOptionsMenu();
    inflater.inflate(R.menu.story, menu);
}

With this I get both Fragment and Activity menu but Duplicates are there.

This should be easy to solve but I am not picking up a way to deal with this. Maybe I didn't understand the life cycle well?

My other approach- Implementing all menus in Fragments will do the trick but this should be our last option.

Solution to this - To maintain both Menu all I have to do is this (Very easy solution):

menu.clear();
inflater.inflate(R.menu.story, menu);
getActivity().getMenuInflater().inflate(R.menu.main, menu);

Problem 2 OnOptionsItemSelected method from 1st fragment is getting called in other fragments.

解决方案

  private void hideAllMenuItems() {
        if (actionBarMenu != null) {
            actionBarMenu.findItem(R.id.action_item1).setVisible(false);
            actionBarMenu.findItem(R.id.action_item2).setVisible(false);
        }
    }


    private void showMenuIcon() {
        if (actionBarMenu != null) {
            hideAllMenuItems();
            if (currentFragment instanceof Fragment1)
                actionBarMenu.findItem(R.id.action_item1).setVisible(true);

            else if (currentFragment instanceof Fragment2)
                actionBarMenu.findItem(R.id.action_item2).setVisible(true);

        }
    }

call shoeMenuIcon() each time new fragment load..

Hope you are looking for this

这篇关于重复片段中的菜单和数据使用FragmentStatePagerAdapter后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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