Android -- listview uses adapter more efficiently
I. Adapter
The adapter is the intermediary between the listview and the data source.
When each piece of data enters the visible area, the adapter calls its getview () method and returns a view representing the specific data. Frequently called during scrolling, supporting thousands of data.
① The simplest method, the slowest and least practical
public void getView(int position , View convertView , ViewGroup parent){ View item = mInflater.inflate(R.layout.list_view, null); (TextView)item.findViewById(R.id.text).setText(DATA[position]); (ImageView)item.findViewById(R.id.img).setImageBitmap(icon); return item; }
② Recycling views using convertview improves the efficiency by 200%
public void getView(int position , View convertView , ViewGroup parent){ if(convertView == null){ convertView = mInflater.inflate(R.layout.list_view, null); } (TextView)convertView.findViewById(R.id.text).setText(DATA[position]);
(ImageView)convertView.findViewById(R.id.img).setImageBitmap(icon);
return convertView; }
③ Use the viewholder mode to Increase the efficiency by 50%
static class ViewHolder{ private TextView tv; private ImageView iv; } public void getView(int position , View convertView , ViewGroup parent){ ViewHolder holder; if(convertView == null){ convertView = mInflater.inflate(R.layout.list_view, null); holder = new ViewHolder(); holder.tv = (TextView)convertView.findViewById(R.id.text); holder.iv = (ImageView)convertView.findViewById(R.id.img); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tv.setText(DATA[position]); holder.iv.setImageBitmap(icon); return convertView; }
Comparison of update efficiency: