Android Viewholder for ViewPager

∥☆過路亽.° 提交于 2019-12-07 03:01:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!