参考:http://blog.csdn.net/bill_ming/article/details/8817172
如果我们有上亿个item要显示怎么办?为每个项目创建一个新视图?NO!这不可能~~~Android实际上为你缓存了视图
Android中有个叫做Recycler(反复循环器)的构件,下图是它的工作原理:
ViewHolder是将多个view封装一个对象
使用View的setTag()方法将ViewHolder的对象缓存起来供下次调用。
public void setTag (Object tag)
Sets the tag associated with this view. A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.
我的理解:ViewHolder 将一些view封装好,ViewHolder没有指定只能被谁使用,ViewHolder 存储的是一组view的对象,具体每个view的内容是什么,并不关心
public View getView(int position, View convertView, ViewGroup parent) { View view; ViewHolder mViewHolder; if (convertView == null || convertView.getTag() == null) { view = mInflater.inflate(R.layout.item_viewpicture_gridview, null); mViewHolder = new ViewHolderGridView(view); view.setTag(mViewHolder); } else { view = convertView; mViewHolder = (ViewHolderGridView)convertView.getTag(); } return view;
public static class ViewHolder {
public ImageView pictureImageView;
public ImageView pictureChooseflag;
public ViewHolderGridView(View view) {
pictureImageView = (ImageView) view.findViewById(R.id.picture_img);
pictureChooseflag = (ImageView) view.findViewById(R.id.picture_choose_flag);
}
}