内ViewPager嵌套片段的OptionsMenu [英] OptionsMenu of Nested Fragments within ViewPager

查看:199
本文介绍了内ViewPager嵌套片段的OptionsMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ActionBarSherlock,我试图实现一个具有viewpager嵌套片段的结构。

我有一个包含一些看法活动和包装fragmet(碎裂)

这碎裂包含显示FragmentA.1,FragmentA.2,FragmentA.3。

视图寻呼机

在默认情况下,onCreateOptionsMenu事件不被分派到孩子的碎片,因为它是
讨论 rel=\"nofollow\">。所以,我使用解决方案,以解决这个问题。

它工作在API级别17伟大的,但下面不显示第一个片段optionsmenu但是当我滚动到其他一切都开始工作就好了。我试图从父片段,但没有结果调用onCreateOptionsMenu。当我滚动回第一个片段它也可以。

有什么建议?

更新:

pssing结构前$ P $的更清晰的方式:

通过包装片段,我的意思持有的viewpager片段。所以结构

 活动
         - > WRAPPER片段(持有viewpager并将childfragmentmanager到适配器(FragmentPagerAdapter)为fragmentmanager)(母公司为活动)
              - > CHILDFRAGMENTS(viewpager的项目)(母公司为包装片段,但viewpager管理它的FrameLayout)

此外,我已经找到了一个临时解决方案,它是不是很好:

 如果(Build.VERSION.SDK_INT> 17){            pager.setCurrentItem(1,FALSE);        }其他{            新android.os.Handler()。postDelayed(新的Runnable(){
                @覆盖
                公共无效的run(){
                    pager.setCurrentItem(1,真);
                }
            },300);
        }


解决方案

也许你该活动的创作结束前初始化你的看法寻呼机。结果
这是问题,因为孩子的碎片创建自己的选项菜单但随后,活动全部无效选项菜单。结果
你必须初始化里面 onActivityCreated您的寻呼机您的包装片段的方法。

I am using ActionBarSherlock and I am trying to implement a nested fragment structure with a viewpager.

I have an activity which contains some views and a wrapper fragmet (FragmentA)

This FragmentA contains a view pager which shows FragmentA.1, FragmentA.2 ,FragmentA.3.

By Default, onCreateOptionsMenu events are not dispatched to child fragments as it is discussed here. So I am using this solution to overcome the issue.

It works great over API level 17, but for below it does not show the optionsmenu for the first fragment but when i scroll to others everything starts to work just fine. I have tried calling onCreateOptionsMenu from parent fragment but no result. It also works when i scroll back to first fragment.

Any suggestions?

Update :

More clear way of expressing the structure :

By wrapper fragment, i meant the fragment which holds the viewpager. So the structure is

ACTIVITY 
        -> WRAPPER FRAGMENT (holds viewpager and passes childfragmentmanager to adapter(FragmentPagerAdapter) as fragmentmanager) (parent is activity)
             -> CHILDFRAGMENTS(items of viewpager) (parent is wrapper fragment but viewpager manages its framelayout)

Also i have found a temp solution which is not so nice :

if(Build.VERSION.SDK_INT > 17){

            pager.setCurrentItem(1,false);

        } else {

            new android.os.Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    pager.setCurrentItem(1, true);
                }
            }, 300);


        }

解决方案

Probably you initialise your view pager before the end of the activity's creation.
This is the problem because child fragments create their options menu but then, activity invalidate all options menu.
You must initialise your pager inside onActivityCreated method of your wrapper fragment.

这篇关于内ViewPager嵌套片段的OptionsMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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