问题
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
回答1:
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.
*/
回答2:
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.
来源:https://stackoverflow.com/questions/7450524/android-viewholder-for-viewpager