本文介绍了ViewHolder模式中的setTag和getTag的工作方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的代码片段,用于实现自定义列表视图.

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()方法感到困惑. 从这个问题中,我来到了知道这是为了在多个对象上建立键/值对,以便我们可以轻松地访问它们.但是,我不明白为什么在这里需要它们……因为我们没有多个持有人对象……只是我们每次必须更改持有人的变量.我们可以在不使用setTaggetTag的情况下在此处进行编码吗?

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?

有人能更好地解释setTaggetTag在这里"做什么吗?

can anyone explain better that what setTag and getTag do "here"?

推荐答案

tag是一种机制,可以使您的views记住某些东西,可能是objectintegerstring或其他任何内容喜欢.

tag is a mechanism to make your views remember something, that could be an object an integer a string or anything you like.

,因此当您第一次创建ListView时,您的convertViewnull.因此您创建一个新的convertView并将该rowobjects的所有references放入viewHolder中.然后将您的viewHolder保存到该convertView( setTag )的内存中. Android将您的convertView放入pool中,将其放入recycle中,然后再次将它放入passes中.但是它的pool可能没有足够的convertViews,因此它再次传递了一个新的convertView那就是null.因此,故事会重复一次,直到androidpool被填满.之后,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的工作方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 11:51