Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Viewholder for ViewPager

Hello I am using a ViewPager but I would now like to retain states, instead of having cells regenerated.

I notice viewpager is very similar to a custom adapter for listviews, and they function similarly where listview regenerate data in cells after they are no longer on screen, viewpager is the same way.

Is there a way to do a viewholder, some kind of object that retains the state of each view, after it is loaded initially, so that the viewpager cells load quicker. (there is some processing and rendering for each cell, but I'll test the limits of the memory myself)

here is my viewpager

like image 599
CQM Avatar asked Sep 16 '11 21:09

CQM


2 Answers

Use FragmentPagerAdapter, which keeps all Fragment objects that have been created actively in memory:

/**
 * Implementation of {@link android.support.v4.view.PagerAdapter} that
 * represents each page as a {@link Fragment} that is persistently
 * kept in the fragment manager as long as the user can return to the page.
 */
like image 104
hackbod Avatar answered Sep 27 '22 18:09

hackbod


Make a hashMap of the layouts after you inflate them. If you already have a layout for corresponding page just replace the layout from hashMap , otherwise inflate the new layout and store it in hashMap. I cannot guarantee if you can restore states. But it will reduce your inflating time every time the user switches between pages.

like image 20
Yashwanth Kumar Avatar answered Sep 27 '22 18:09

Yashwanth Kumar