Hold View Objects in a View Holder
Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.
A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. First, you need to create a class to hold your exact set of views. For example:
static class ViewHolder{
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}
Then populate the ViewHolder and store it inside the layout.
// when convertView is null
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);
Now you can easily access each view without the need for the look-up, saving valuable processor cycles.
posted on 2013-11-12 15:22 iwant2know 阅读(78) 评论(0) 收藏 举报
浙公网安备 33010602011771号