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

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

 

最近在使用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返回就可以解决这个问题。

 https://www.jianshu.com/p/c04fff51c69f  侵删

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值