Search code examples
androidandroid-viewpager

View Holder Pattern for ViewPager?


I have a ViewPager, and FragmentPagerAdpater and 3 Fragments to implement an tab swipe view. Is the ViewPager supposed to "remember" the 3 views after first time rendering?

My problem is that after some time, the layouts of the 3 views are "forgotten". That means I need to update them again and user will see the ugly part before updating.

Is there a view holder pattern for viewpager as well? What technique can i use to remain those already rendered views or preventing them being destroyed?


Solution

  • You can use ViewPager setOffscreenPageLimit() to control how many pages are kept in memory and pre-created before being scrolled to. By default its value is 1, meaning it will keep the current page in memory and the pages just before and after it. Depending on your page size (full-screen or partial screen), increasing it to e.g. 2 will prevent the page being constructed on-screen.