当前位置:网站首页>Fragment中嵌套ViewPager数据空白页异常问题分析

Fragment中嵌套ViewPager数据空白页异常问题分析

2022-08-09 12:04:00 谢栋_

         开题思考:Fragment放ViewPager,ViewPager里面是fragment。第一次进入没问题,再次进入ViewPager的fragment时里面内容就没了,出现数据空白页异常问题。

          描述:activity中放了两个Fragment,分别为待审核页、已加入页;其中待审核fragment中利用viewpager放了三个fragment,分别为全部、审核加入、审核退出,第二次切换待审核、已加入fragment时,会发现待审核fragment中ViewPager承载的Fragment会出现数据空白页现象。如下图所示:

         根据经验感觉是FragmentManager出了问题,因为FragmentManager的职责本来就是负责管理Fragment,包括对Fragment栈中的Fragement进行添加,删除,替换等操作,那既然出现了空白页,肯定是FragmentManager在进行Fragment替换时没有正常工作,查看Fragment以及FragmentActivity的源码发现有三种类型的FragmentManager,分别为getFragmentManager、getSupportFragmentManager、getChildFragmentManager;其中getFragmentManager跟getChildFragmentManager在Fragment类中,而getSupportFragmentManager位于FragmentActivity中,而getFragmentManager虽然在Fragment中,但是官方注释中最后有提到,如果当前Fragment是另外一个Fragment的子类,则当前FragmentManager属于父类,这就意味着,三者虽然相似但管理的对象却是不同的

三者源码如下

getFragmentManager:

    /**
     * Return the FragmentManager for interacting with fragments associated
     * with this fragment's activity.  Note that this will be non-null slightly
     * before {@link #getActivity()}, during the time from when the fragment is
     * placed in a {@link FragmentTransaction} until it is committed and
     * attached to its activity.
     *
     * <p>If this Fragment is a child of another Fragment, the FragmentManager
     * returned here will be the parent's {@link #getChildFragmentManager()}.
     */
    final public FragmentManager getFragmentManager() {
        return mFragmentManager;
    }

getSupportFragmentManager:

    /**
     * Return the FragmentManager for interacting with fragments associated
     * with this activity.
     */
    public FragmentManager getSupportFragmentManager() {
        return mFragments.getSupportFragmentManager();
    }


getChildFragmentManager:


    /**
     * Return a private FragmentManager for placing and managing Fragments
     * inside of this Fragment.
     */
    final public FragmentManager getChildFragmentManager() {
        if (mChildFragmentManager == null) {
            instantiateChildFragmentManager();
            if (mState >= RESUMED) {
                mChildFragmentManager.dispatchResume();
            } else if (mState >= STARTED) {
                mChildFragmentManager.dispatchStart();
            } else if (mState >= ACTIVITY_CREATED) {
                mChildFragmentManager.dispatchActivityCreated();
            } else if (mState >= CREATED) {
                mChildFragmentManager.dispatchCreate();
            }
        }
        return mChildFragmentManager;
    }



       根据google官方的描述(PS:English自译拿不出手),大意为getFragmentManager、跟getSupportFragmentManager是为了获取能管理和当前Activity有关联的Fragment的FragmentManager,也就是说此二者是属于Activity的,再来看下getChildFragment的官方注释

    /**
     * Return a private FragmentManager for placing and managing Fragments
     * inside of this Fragment.
     */
    final public FragmentManager getChildFragmentManager() {
   

         上文注释大致意思返回一个私有的FragmentManager来管理Fragment,它属于Fragment。至此整个问题就梳理清楚了,因为文章开题提出的问题,是在Fragment中使用ViewPager装载Fragment,也就是说,在Fragment中应该是用的是Fragemnt类中提供的manager方法,即在给ViewPager设置FragmentManager的时候应该使用getChildFragmentManager。

文章开题提到的给ViewPager设置Adapter

 viewPager.setAdapter(new ManageFragmentPagerAdapter(getFragmentManager(), list));

经过分析应该修改为

 viewPager.setAdapter(new ManageFragmentPagerAdapter(getChildFragmentManager(), list));

    归纳总结getFragmentManager()所得到的是所在fragment 的父容器的管理器,而getChildFragmentManager()所得到的是在fragment  里面子容器的管理器,即getFragmentManager到的是activity对所包含fragment的Manager,而如果是fragment作为容器装载Fragment,就需要利用getChildFragmentManager()了





原网站

版权声明
本文为[谢栋_]所创,转载请带上原文链接,感谢
https://xiedong.blog.csdn.net/article/details/78842990