美文网首页
使用FragmentPagerAdapter导致切换时出现空白

使用FragmentPagerAdapter导致切换时出现空白

作者: Rreply | 来源:发表于2018-09-02 13:49 被阅读259次

    最近在使用Tablayout + viewPager + fragment的时候遇到了一个问题。
    设计的应用总共有三个tab,分别是时间线A、收藏B、稍后阅读C。
    打开应用的时候首先进入A,此时A能够正常显示,然后切换到B时,B也能够正常显示,再然后切换到C的时候,C也能够正常显示。
    接下来就出现问题了。
    从C切换到B的时候,B能够正常显示,再切换到A的时候,A显示空白,此时切换到B,B正常显示,然后再切换到C的时候,C也显示空白。
    消失的原因为:FragmentStatePageAdapter与FragmentPageAdapter的区别

    解决方法一:

    FragmentPageAdapter替换为FragmentStatePageAdapter。但是这个方法有一个不好之处,因为在切换时需要重新生成新的fragment,在遇到一些比较复杂的fragment时候能够明显的感到卡顿延迟。

    解决方法二:

            mViewPager.setAdapter(viewPagerAdapter);
            //设置缓存的视图数量为2
            mViewPager.setOffscreenPageLimit(2);
    

    调用setOffscreenPageLimit()方法设置缓存的数量
    下面我们看看这个方法的源码

        /**
         * Set the number of pages that should be retained to either side of the
         * current page in the view hierarchy in an idle state. Pages beyond this
         * limit will be recreated from the adapter when needed.
         *
         * <p>This is offered as an optimization. If you know in advance the number
         * of pages you will need to support or have lazy-loading mechanisms in place
         * on your pages, tweaking this setting can have benefits in perceived smoothness
         * of paging animations and interaction. If you have a small number of pages (3-4)
         * that you can keep active all at once, less time will be spent in layout for
         * newly created view subtrees as the user pages back and forth.</p>
         *
         * <p>You should keep this limit low, especially if your pages have complex layouts.
         * This setting defaults to 1.</p>
         *
         * @param limit How many pages will be kept offscreen in an idle state.
         */
        public void setOffscreenPageLimit(int limit) {
            if (limit < DEFAULT_OFFSCREEN_PAGES) {
                Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to "
                        + DEFAULT_OFFSCREEN_PAGES);
                limit = DEFAULT_OFFSCREEN_PAGES;
            }
            if (limit != mOffscreenPageLimit) {
                mOffscreenPageLimit = limit;
                populate();
            }
        }
    

    从注释中可以看出该方法设置的是显示的视图两侧各缓存的fragment数量。我总共使用了三个fragment,所以将该值设置为2的时候就能够保证无论如何剩下的两个fragment都能够缓存。

    解决方法三:

    FragmentPagerAdapteronDestroyItem()重写方法中将super.onDestrotItem()注释掉。这个方法本质上和方法二差不多,是将Fragment的视图保存下来,来达到所有的fragment都完整的缓存下来,其相当于setOffscreenPageLimit(∞)

    解决方法四:

    在fragment的onCreateView()方法中添加如下代码:

    if (mView != null) {
                ViewGroup parent = (ViewGroup) mView.getParent();
                if (parent != null) {
                    parent.removeView(mView);
                }
                return mView;
            }
    

    因为FragmentPagerAdapter在不使用某个Fragment的时候是销毁了其视图,所以当这个fragment从缓存中重新使用的时候,要调用其onCreateView()方法,而原来的view没有被回收,新生成的view覆盖了原来的view,因此页面显示空白。我们这里手动回收原来的view,然后把这个原来的view作为新生成的view返回就可以解决这个问题。

    相关文章

      网友评论

          本文标题:使用FragmentPagerAdapter导致切换时出现空白

          本文链接:https://www.haomeiwen.com/subject/khguwftx.html