问题描述
我今天学会了Android的阵列,发现有一个孔模式,它使用视图持有人保存视图的参考,而不是每次调用findviewbyid.
但是它如何工作?适配器通常用于显示视图列表(组)S,如果我缓存视图,为什么它们都不全部参考最旧的一个?
推荐答案
如果您想要有关视图持有人的工作方式的最佳解释,请查看Romain Guy的Google i/o 2009在 youtube ,特别是最初的15分钟.
简而言之,Adapter用作基础数据与ViewGroup之间的链接.它将渲染尽可能多的View s来填充屏幕.滚动或其他任何推动View的事件不在屏幕之外,Adapter将重复使用View,View填充了正确的数据,将在屏幕上渲染.
getView(int pos, View view, ViewGroup parent)方法将随时使用正确的View,无论您的布局如何.我不知道它的内部内容,但是我敢肯定您可以浏览任何适配器的源代码(例如
对不起,但丹尼斯的回答可能是错误的.
实际上,视图实例(和视图持有人)是的数量,就像您的屏幕可以显示. 如果您的屏幕看起来像: 您将有4个意见实例.如果滚动屏幕,则第一个将消失,但可以通过convertView将其传递给您创建第五项. 因此,您可以使用第一视图持有人中的参考. 我相信列表视图下面的工作就是这样(考虑到我们只有一个项目视图类型): 做一次: 重复每个项目: 因此,您具有从XML布局中夸大的视图,可以重复使用以绘制多个列表项目.查看持有人通过保存getViewById查找来加快速度.其他推荐答案
[list view]
the first item
the second item
the third item
the fourth item
其他推荐答案
inflate item view from layout, cache it
ask adapter to fill the data into the view
draw the view on the screen
move to next item
问题描述
I learned Android's ArrayAdapter today, and find there is a commom pattern which uses a ViewHolder to hold Views' reference instead of calling findViewById everytime.
But how does it work? Adapter is usually used to display a list of View(Group)s, If I cache the View, why don't they all reference to the oldest one?
推荐答案
If you want the best explanation on how the ViewHolder works, check out Romain Guy's Google I/O 2009 talk in youtube , specially the first 15 minutes.
In short, the Adapter functions as a link between the underlying data and the ViewGroup. It will render as many Views as required to fill the screen. Upon scrolling or any other event that pushes a View is out of the screen, the Adapter will reuse that View, filled with the correct data, to be rendered at the screen.
The getView(int pos, View view, ViewGroup parent) method will use the right View at any time, regardless of your layout. I do not know the internals of this, but I'm sure you can browse the source code for any adapter (such as ArrayAdapter.java) if you're interested.
The ViewHolder just keeps a pointer to the Views as obtained by view.findViewById(int id). It is the Adapter responsibility to return the right data corresponding to any position.
Slides 11 to 13 of Romain's presentation will make it a lot more clear than anything I can write.
其他推荐答案
Sorry but denis' answer may be wrong. In fact, the view instances(and ViewHolders) are as many as your screen can display.
If your screen looks like:
[list view] the first item the second item the third item the fourth item
You will have 4 instances of views. If you scroll screen, the first will disappear but be pass to getItem() as convertView for you to create the fifth item.
So you can use the references in first ViewHolder.
其他推荐答案
I believe the work beneath the list view is something like this (considering we have only one item view type):
do once:
inflate item view from layout, cache it
repeat for every item:
ask adapter to fill the data into the view draw the view on the screen move to next item
so you have the view which is inflated from xml layout and can be reused for drawing multiple list items. ViewHolder speeds it up a bit more by saving getViewById lookups.