How do I implement view recycling mechanism for PagerAdapter?(如何实现 PagerAdapter 的视图回收机制?)
问题描述
我有一个寻呼机适配器,它可以扩展表示日历的复杂视图.
I have a pager adapter that suppose to inflate a complex view representing a calendar.
每年膨胀日历大约需要 350 毫秒.
It takes around ~350 ms to inflate each year of the calendar.
为了提高性能,我想实现与回收视图的 ListView
数组适配器中存在的相同机制(getView() 中的
convertView
参数).
To improve performance I would like to implement the same mechanism that exists in the ListView
array adapter of recycling views (convertView
parameter in getView()
).
这是我当前来自适配器的 getView()
.
Here is my current getView()
from the adapter.
实现这种行为的任何指针或方向?特别是我如何在适配器中知道其中一个 DateTileGridViews
正在从屏幕上滑动,以便我可以将其保存在内存中以供下次重用.
Any pointers or direction for implementing such a behavior?
In particular how can I know in the adapter that one of the DateTileGridViews
is being swiped of the screen so I could save it in memory to reuse it next time.
推荐答案
所以我想通了.
- overwrite
destroyItem(ViewGroup container, int position, Object view)
ans save you cached view - 创建一个单独的方法,看看是否有机会使用回收的视图,或者是否应该扩充一个新的视图.
- 记得在缓存中删除回收的视图,以避免相同的视图将相同的视图附加到寻呼机.
- overwrite
destroyItem(ViewGroup container, int position, Object view)
ans save you cached view
- create a separate method to see if there is any chance to use a recycled view or should you inflate a new one.
- remember to remove the recycled view from cache once its been used to avoid having same view attaching same view to the pager.
这里是代码.. 我使用 Stack of view 来缓存从我的寻呼机中删除的所有视图
here is the code.. I used a Stack of view to cache all removed views from my pager
不要忘记在适配器构造函数中实例化堆栈.
这篇关于如何实现 PagerAdapter 的视图回收机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!