0

以下示例代码来自网站,我认为 holder = (ViewHolder) convertView.getTag() 应该是 holder = (ViewHolder) convertView,对吧?谢谢!

 public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;    
        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }    
        public int getCount() {
            return count;
        }    
        public Object getItem(int position) {
            return position;
        }    
        public long getItemId(int position) {
            return position;
        }            
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);    
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.checkbox.setId(position);
            holder.imageview.setId(position);
            holder.checkbox.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CheckBox cb = (CheckBox) v;
                    int id = cb.getId();
                    if (thumbnailsselection[id]) {
                        cb.setChecked(false);
                        thumbnailsselection[id] = false;
                    } else {
                        cb.setChecked(true);
                        thumbnailsselection[id] = true;
                    }
                }
            });
            holder.imageview.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = v.getId();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file://" + arrPath[id]),
                            "image/*");
                    startActivity(intent);
                }
            });
            holder.imageview.setImageBitmap(thumbnails[position]);
            holder.checkbox.setChecked(thumbnailsselection[position]);
            holder.id = position;
            return convertView;
        }
    }

    class ViewHolder {
        ImageView imageview;
        CheckBox checkbox;
        int id;
    }
}
4

1 回答 1

1

我认为 holder = (ViewHolder) convertView.getTag() 应该是 holder = (ViewHolder) convertView,

既然ViewHolder不是View演员阵容怎么会起作用?您将ViewHolder对象设置为 的标记convertView,并在每次getView调用时检索它,以避免查找您的视图组件(您的ImageViewCheckBox)。这样你就可以避免 findViewById() 在滚动的过程中调用ListView

于 2013-07-10T08:44:54.490 回答