1、ListView 如何提高其效率?
**当 convertView 为空时,用 setTag()方法为每个 View 绑定一个存放控件的 ViewHolder 对象
convertView不为空, 重复利用已经创建的view的时候, 使用getTag()方法获取绑定的ViewH
对象,这样就避免了findViewById对控件的层层查询,而是快速定位到控件。**
① 复用ConvertView,使用历史的view,提升效率200%
② 自定义静态类ViewHolder,减少findViewById 的次数。提升效率50%
③ 异步加载数据,分页加载数据。
④ 使用WeakRefrence引用ImageView对象
关键代码
`public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
TextView textView = null;
if(convertView == null){ //① 复用ConvertView,使用历史的view,提升效率200%
convertView = layoutInflater.inflate(listviewItem, null);
imageView = (ImageView) convertView.findViewById(R.id.imageView1);
textView = (TextView) convertView.findViewById(R.id.textView1);
//② 自定义静态类ViewHolder,减少findViewById 的次数。提升效率50%
convertView.setTag(new DataWrapper(imageView, textView));
}else{
DataWrapper dataWrapper = (DataWrapper) convertView.getTag();
imageView = dataWrapper.imageView;
textView = dataWrapper.textView;
}
Contact contact = data.get(position);
textView.setText(contact.name);
//异步加载图片数据
asyncImageLoad(imageView, contact.image);
return convertView;
}`