问题描述
我有一个简单的代码片段,用于实现自定义列表视图.
I have a simple code snippet for implementing custom listview.
我的代码如下:
WeatherAdapter.java:
public class WeatherAdapter extends ArrayAdapter<weather>{
Context mcontext;
int mlayoutResourceId;
weather mdata[] = null;
View row;
public WeatherAdapter(Context context, int layoutResourceId, weather[] data) {
super(context, layoutResourceId, data);
mlayoutResourceId = layoutResourceId;
mcontext = context;
mdata = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ( (Activity) mcontext).getLayoutInflater();
row = inflater.inflate(mlayoutResourceId, parent, false);
holder = new WeatherHolder(row);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
weather w = mdata[position];
holder.txtTitle.setText(w.mtitle);
holder.imgIcon.setImageResource(w.micon);
return row;
}
WeatherHolder.java :
class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
public WeatherHolder(View v){
imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
txtTitle = (TextView)row.findViewById(R.id.txtTitle);
}
}
}
我在SO和其他站点上看到了很多答案,并且我了解了listview的回收机制.
I have seen so many answers on SO and other sites and I understood the recycling mechanism of listview.
我还了解到,从Viewholder中,我们可以将子视图保存在适配器中,而不必多次调用findViewById()
.因此,这是为了优化.
I also understood that from viewholder, we can hold the child views in the adapter and we do not have to call findViewById()
many times. So, it is for optimization.
但是我只对setTag(holder)
和getTag()
方法感到困惑. 从这个问题中,我来到了知道这是为了在多个对象上建立键/值对,以便我们可以轻松地访问它们.但是,我不明白为什么在这里需要它们……因为我们没有多个持有人对象……只是我们每次必须更改持有人的变量.我们可以在不使用setTag
和getTag
的情况下在此处进行编码吗?
But I have only the confusion in setTag(holder)
and getTag()
methods. From this question, I came to know that it is for making a key-value pair on multiple objects, so that we can access them easily. But, I do not understand why they are required here...because, we do not have multiple holder objects...only we have to change holder's variables each time. can we code here without using setTag
and getTag
?
有人能更好地解释setTag
和getTag
在这里"做什么吗?
can anyone explain better that what setTag
and getTag
do "here"?
推荐答案
tag
是一种机制,可以使您的views
记住某些东西,可能是object
,integer
,string
或其他任何内容喜欢.
tag
is a mechanism to make your views
remember something, that could be an object
an integer
a string
or anything you like.
,因此当您第一次创建ListView
时,您的convertView
是null
.因此您创建一个新的convertView
并将该row
的objects
的所有references
放入viewHolder
中.然后将您的viewHolder
保存到该convertView
( setTag )的内存中. Android
将您的convertView
放入pool
中,将其放入recycle
中,然后再次将它放入passes
中.但是它的pool
可能没有足够的convertViews
,因此它再次传递了一个新的convertView
那就是null
.因此,故事会重复一次,直到android
的pool
被填满.之后,android
从其池中获取一个convertView
并将其传递给您.您会发现它不是null
,所以您问它我第一次给您的对象references
在哪里? ( getTag ),因此您将获得它们并随心所欲.
so when your ListView
is going to create for the first time your convertView
is null
. so you create a new convertView
and put all of your references
of the objects
of that row
in a viewHolder
. then save your viewHolder
into the memory of that convertView
(setTag). Android
takes your convertView
and puts it in its pool
to recycle
it and passes
it again to you. but its pool
may not have enough convertViews
so it again passes a new convertView
thats null
. so again the story is repeated till the pool
of android
is filled up. after that android
takes a convertView
from its pool and passes it to you. you will find that its not null
so you ask it where are my object references
that I gave to you for the first time? (getTag) so you will get those and do whatever you like.
在下面的行中进行详细说明
but its pool may not have enough convertViews so it again passes a new convertView thats null
android pool
为空.因此对于listView
的第一项,它会向您发送一个必须显示的convertView
.之后,android
将其保存在其pool
中,因此其pool
现在仅包含一个convertView
.对于要创建android的listView
的第二项,无法使用其池,因为它实际上具有一个元素,并且该元素是您的第一项,并且现在正在显示,因此它必须通过另一个convertView
.重复此过程,直到android
在其pool
中发现一个convertView
不再显示,并将其传递给您.
android pool
is empty when your listView
is going to create. so for the first item of your listView
it sends you a convertView
that must be displayed. after that android
saves it in its pool
, so its pool
now contains just one convertView
. for your second item of your listView
that is going to create android can not use its pool because it is actually has one element and that element is your first item and it is being shown right now so it has to pass another convertView
. this process repeates until android
found a convertView
in its pool
thats not being displayed now and passes it to you.
Android会在滚动列表时使用行号,直到每一行都填满为止.
Android inflates each row till the screen filled up after that when you scroll the list it uses holder.
这篇关于ViewHolder模式中的setTag和getTag的工作方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!