问题
I have 3 fragments that are managed by an FragmentPagerAdapter, set to a viewPager.
I want to load fragments one by one, but when the onCreate method of FragmentActivity is executed, the 2 first fragments are executed (onCreateView method).
I have tried to limit fragments loading with the setOffscreenPageLimit method but nothing change.
mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
pager = (ViewPager) super.findViewById(R.id.tabviewpager);
pager.setOffscreenPageLimit(0);
pager.setAdapter(this.mPagerAdapter);
Thank you for your help .
回答1:
The minimum for OffscreenPageLimit
is set to 1
in the ViewPager
source code:
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();
}
}
来源:https://stackoverflow.com/questions/13560550/android-limit-fragments-loading-with-a-viewpager