最佳答案
我在 ViewPager
实例中使用的片段是资源密集型的,因此我希望一次只加载一个片段。当我尝试以下方法时:
mViewPager.setOffscreenPageLimit(0);
mViewPager.setAdapter(mPagerAdapter);
我的 FragmentStatePagerAdapter.getItem(int position)
覆盖函数被调用3次,这就是我调用 mViewPager.setOffscreenPageLimit(1)
时发生的情况。我希望它只被调用一次,因为我指定了0个离屏页面。
我相信我调用的一切都是正确的,因为如果我调用 mViewPager.setOffscreenPageLimit(2)
,FragmentStatePagerAdapter.getItem(int position)
的调用次数是我预期的5次。
ViewPager 是否需要至少1个离屏页面,或者我在这里做错了什么?