Android ViewHolder简介
在代码之中大量的使用findViewById是一件十分消耗性能的事。因此建议多使用ViewHolder。
我们先来看一下ViewHolder的简单实现:
static class ViewHolder { TextView text; TextView timestamp; ImageView icon; ProgressBar progress; int position; }
在这个ViewHolder类之中有TextView类型成员变量,ImageView类型成员变量,ProgressBar以及一个整形数据;
然后我们可以通过下面方式使用它:
ViewHolder holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image); holder.text = (TextView) convertView.findViewById(R.id.listitem_text); holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp); holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner); convertView.setTag(holder);
我们可以看到,将concertView中的TextView,ImageView,ProgressBar赋给ViewHolder的d对象的相应成员属性。
关于Tags,我们看下官方文档:
Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view.
Tags是用于存放view的附加信息的内容。因此现在很容易理解:
convertView.setTag(holder);
就是说给convertView设置额外的数据,这个数据之中有TextView,ImageView,ProgressBar类型数据。

浙公网安备 33010602011771号