Maciej Boguta
Maciej Boguta

Reputation: 1364

ViewPager FragmentStatePagerAdapter keep more than three fragments in memory

The problem is ive got sort of book reader, landscape shows two pages and portrait just one. For some reason I blocked destroying activities on config change. the story is pretty much the same like here ViewPager + FragmentStatePagerAdapter + orientation change. Although the question is bit different - Id wish to store not 3 but 5 fragments. its about rendering time, Im easily handling swipe stuffs with adding +-1 in portrait and +-2 in landscape, though the problem is, in landscape orientation, it wont render proper next / previous fragments. As obviously its rendering +1 and -1 items, I want +2/-2 items to be instantiated instead. So theres either some kind of solution that changes positions to instantiate in landscape (which im not aware of though), or storing 5 items in an adapter instead of just 3. Anyone with any sort of proper solution?

Upvotes: 2

Views: 2874

Answers (2)

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

try this: mViewPager.setOffscreenPageLimit(4);

Upvotes: 2

Nabz
Nabz

Reputation: 390

You can store 5 items in the adapter by setting the OffscreenPageLimit of the ViewPager to 4:

viewPager.setOffscreenPageLimit(4);

hope that I've helped you.

Upvotes: 12

Related Questions