问题描述
我在 ViewPager
实例中使用的片段非常耗费资源,所以我只想一次加载一个.当我尝试以下操作时:
The fragments I use in my ViewPager
instance are quite resource intensive, so I'd only like to load one at a time. When I try the following:
mViewPager.setOffscreenPageLimit(0);
mViewPager.setAdapter(mPagerAdapter);
我的 FragmentStatePagerAdapter.getItem(int position)
覆盖函数被调用了 3 次,这就是我调用 mViewPager.setOffscreenPageLimit(1)
时发生的情况.我希望它只被调用一次,因为我指定了 0 个屏幕外页面.
My FragmentStatePagerAdapter.getItem(int position)
override function is called 3 times, which is what happens when I call mViewPager.setOffscreenPageLimit(1)
. I would expect it to only be called once, because I specified 0 offscreen pages.
我相信我正确调用了所有内容,因为如果我调用 mViewPager.setOffscreenPageLimit(2)
,FragmentStatePagerAdapter.getItem(int position)
会被调用 5 次会期望.
I believe I'm calling everything correctly, because if I call mViewPager.setOffscreenPageLimit(2)
, FragmentStatePagerAdapter.getItem(int position)
is called 5 times as I would expect.
ViewPager 是否需要至少 1 个屏幕外页面,或者我在这里做错了什么?
Does ViewPager require a minimum of 1 offscreen pages, or am I doing something wrong here?
推荐答案
是的.如果我正确阅读了源代码,您应该会在 LogCat 中收到关于此的警告,例如:
Yes. If I am reading the source code correctly, you should be getting a warning about this in LogCat, something like:
Requested offscreen page limit 0 too small; defaulting to 1
这篇关于ViewPager.setOffscreenPageLimit(0) 无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!