Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 875
xiangxin Avatar asked Sep 11 '12 10:09

xiangxin


2 Answers

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.

like image 95
laalto Avatar answered Sep 22 '22 12:09

laalto


In your tabs adapter just override the destroyItem() method & quote out the super statement

this will save the instance of your fragments and they wont need to load again and again

@Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        // super.destroyItem(container, position, object);
    }

something like this.

like image 33
Aalok Sharma Avatar answered Sep 22 '22 12:09

Aalok Sharma