本文介绍了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了解到,我们可以在adapter中保存子视图,不需要多次调用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 记住一些东西的机制,这可能是一个 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 第一次创建时,你的 convertViewnull.所以你创建一个新的 convertView 并将你所有的 referencesobjectsrow 放在一个 中>viewHolder.然后将您的 viewHolder 保存到那个 convertView(setTag) 的内存中.Android 获取您的 convertView 并将其放入其 poolrecyclepasses再给你吧.但是它的 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.

下面一行的详细说明

但它的池可能没有足够的convertViews,所以它再次传递一个新的convertView,它为空

android pool 在您的 listView 将要创建时为空.因此,对于 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 的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 19:01