我有3个片段由FragmentPagerAdapter管理,设置为viewPager.
我想逐个加载片段,但是当执行FragmentActivity的onCreate方法时,会执行2个第一个片段(onCreateView方法).
我试图用setOffscreenPageLimit方法限制片段加载但没有任何改变.
mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(),fragments); pager = (ViewPager) super.findViewById(R.id.tabviewpager); pager.setOffscreenPageLimit(0); pager.setAdapter(this.mPagerAdapter);
谢谢您的帮助 .
解决方法
在ViewPager
source code中,OffscreenPageLimit的最小值设置为1:
private static final int DEFAULT_OFFSCREEN_PAGES = 1; .... 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(); } }