分类: Android平台
2014-05-19 15:04:28
ListView缓存机制
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.type_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.app_icon);
holder.textView = (TextView)convertView.findViewById(R.id.app_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.imageView.setImageResource(R.drawable.index_default_image);
holder.textView.setText("");
return convertView;
}
/**
* ViewHolder
*/
static class ViewHolder {
ImageView imageView;
TextView textView;
}
通过convertView是否为null减少layout inflate次数,通过静态的ViewHolder减少findViewById的次数,这两个函数尤其是inflate是相当费时间的。