Recycling views in custom array adapter: how exactly is it handled?

后端 未结 5 881
栀梦
栀梦 2020-12-15 11:15

I am having an unclear issue concerning the recycling of views in a getView method of a custom array adapter.

I understand that elements are reused, but how do I kn

5条回答
  •  北海茫月
    2020-12-15 11:56

    You want to create a ViewHolder class in your MainActivity. Something like

     static class ViewHolder
        {
            TextView tv1;
            TextView tv2;
        }
    

    then in your getView, the first time you get your Views from your xml in the if and reuse them after that in the else

    View rowView = convertView;
            if (rowView == null)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflater.inflate(R.layout.layout_name_to_inflate, parent, false);
                holder = new ViewHolder();
                holder.tv1= (TextView) rowView.findViewById(R.id.textView1);
                holder.tv2 = (RadioGroup) rowView.findViewById(R.id.textView2);             
                rowView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) rowView.getTag();
            }
    

提交回复
热议问题