Reputation:
I'm using ViewPager with Fragments. I have offset set to 1 (I know it's default) but it still loads three pages at the start (0 -> 1 -> 2) instead of two (0 -> 1)... What could be the problem here?
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
...
mPager = (ViewPager) findViewById(R.id.viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOffscreenPageLimit(1);
...
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new InitializationFragment();
} else if (position == 8) {
return new InitializationFragment();
} else if (position == 9) {
return new SummaryFragment();
} else {
return new IngredientFragment(PizzaStage.values()[position - 1], layout);
}
}
@Override
public int getCount() {
return NUM_PAGES; //10
}
}
Upvotes: 0
Views: 1699
Reputation: 30985
ViewPager
will always try to pre-load pages to the right and/or to the left of the current page, so that when the user swipes, there is no delay from creating the view.
If you want to disable this behavior, you can call viewPager.setOffscreenPageLimit(0)
.
Usually all you want to find out is when the view has been swiped and is now the current view.
android - How to determine when Fragment becomes visible in ViewPager - Stack Overflow
Upvotes: 1
Reputation: 2866
Are you referring setOffscreenPageLimit as offset? If you defined NUM_PAGES as 10 in adapter, all those 10 will be loaded. As for setOffscreenPageLimit, you use it to set number of pages that you want to retain in idle state. Not limiting the fragments that need to be loaded. If you have 5 fragments and you don't want android to recreate those fragments everytime you swipe left/right, then you need to use setOffscreenPageLimit.
Upvotes: 0