setOffscreenPageLimit()如何通过保留更多的屏幕外片段来提高ViewPager性能? [英] How does setOffscreenPageLimit() improve ViewPager performance by retaining more offscreen Fragments?

查看:312
本文介绍了setOffscreenPageLimit()如何通过保留更多的屏幕外片段来提高ViewPager性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager 控制五个 Fragments 。当我从索引1的 Fragment 滑动到索引0的 Fragment 时,动画中有短暂的停顿我想消除。

I have a ViewPager controlling five Fragments. When I swipe from Fragment at index 1 to Fragment at index 0, there's a brief pause in the animation that I'd like to eliminate.

当前,我没有调用 setOffscreenPageLimit() ,所以我知道 Fragment 0正在由 ViewPager 保留在内存中,而我在<$ c $上c> Fragment 1处于空闲状态,因为屏幕外页面限制默认为1(当前 Fragment 的任一侧)。

Currently, I'm not calling setOffscreenPageLimit(), so I know that Fragment 0 is being held in memory by the ViewPager while I'm on Fragment 1 in its idle state, because the off screen page limit defaults to 1 (one on either side of the current Fragment).

在这里变得令人困惑。如果我在 ViewPager 上调用 setOffscreenPageLimit(4)从1到0动画滑动的暂停不见了-动画很流畅

Here's where it gets confusing. If I call setOffscreenPageLimit(4) on my ViewPager, the pause in the swipe from 1 to 0 animation is gone -- the animation is smooth.

因此,我得出结论,以某种方式保留 Fragments 内存中的2-4可将动画从 Fragment 1改进为 Fragment 0。

Therefore, I conclude that somehow, keeping Fragments 2-4 in memory improves the animation from Fragment 1 to Fragment 0.

如何保留2-4个片段可以将滑动动画从 Fragment 1提高到片段 0?

How does retaining Fragments 2-4 improve the swiping animation from Fragment 1 to Fragment 0?

编辑

我运行TraceView,使用 onPageScrollStateChanged(int state)确定何时启动和停止跟踪,如下所示:

I ran TraceView, using onPageScrollStateChanged(int state) to determine when to start and stop the trace, as follows:

@Override
public void onPageScrollStateChanged(int state) {
    if (state == 1 && mViewPager.getCurrentItem() == 1) {
        Debug.startMethodTracing("ViewPagerTesting", 100000000);
    }

    if (state == 0 && mViewPager.getCurrentItem() == 0) {
        Debug.stopMethodTracing();
    }
}

看来, ViewPager 的可访问性方法导致UI线程冻结。当我在ViewPager上调用 setOffscreenPageLimit(4)时,这些可访问性方法已步履蹒跚-花费的时间可忽略不计。

It appears that the ViewPager's accessibility methods are causing the UI thread to freeze. When I call setOffscreenPageLimit(4) on the ViewPager, these accessibility methods are way down in the trace -- taking negligible time to finish.

造成延迟的原因是什么?

What's causing the delay?

推荐答案

使用默认实现 setOffscreenPageLimit()它仅加载它右边的一个片段。例如。当您使用索引1时,它已在内存中加载了索引2,但未加载索引0,因此向左滑动将不得不从头开始生成一个新片段。要检验此理论,您可能需要使用 setOffscreenPageLimit(2),然后尝试滑动索引1-> 0。这绝不是最佳解决方案,但会帮助您消除对这一概念的疑问。

When you use the default implementation of setOffscreenPageLimit() it is only loading the one fragment which is to the right of it. For eg. when you are on index 1, it has index 2 loaded in memory but not index 0, so swiping left will have to generate a new fragment from scratch. To test this theory you might want to use setOffscreenPageLimit(2) and then try swiping index 1->0. This in no way is the optimal solution but will help clear out your doubts about the concept.

这篇关于setOffscreenPageLimit()如何通过保留更多的屏幕外片段来提高ViewPager性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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