Android Viewholder for ViewPager

Hello, I am using ViewPager, but now I would like to save the states, instead of regenerating the cells.

I see that the viewpager is very similar to the user adapter for listviews, and they work similarly, when listview regenerates the data in the cells after they no longer appear on the screen, the viewpager is the same.

Is there a way to make a view object, some kind of object that saves the state of each view after it is loaded initially, so that the viewpager cells load faster. (there is some processing and rendering for each cell, but I will check the memory limits myself)

here is my viewer

+6
source share
2 answers

Use the FragmentPagerAdapter, which saves all Fragment objects that have been actively created 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. */ 
+8
source

Create a hash map of the layouts after they are inflated. If you already have a layout for the corresponding page, just replace the layout from hashMap, otherwise inflate the new layout and save it in hashMap. I can’t guarantee if conditions can be restored. But this will reduce the bloat time each time the user switches between pages.

+2
source

Source: https://habr.com/ru/post/897499/


All Articles