ListView中convertView和ViewHolder的工作原理
ListView和Adapter
1 public class MultipleItemsList extends ListActivity { 2 3 private MyCustomAdapter mAdapter; 4 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 mAdapter = new MyCustomAdapter(); 9 for (int i = 0; i < 50; i++) { 10 mAdapter.addItem("item " + i); 11 } 12 setListAdapter(mAdapter); 13 } 14 15 private class MyCustomAdapter extends BaseAdapter { 16 17 private ArrayList mData = new ArrayList(); 18 private LayoutInflater mInflater; 19 20 public MyCustomAdapter() { 21 mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 22 } 23 24 public void addItem(final String item) { 25 mData.add(item); 26 notifyDataSetChanged(); 27 } 28 29 @Override 30 public int getCount() { 31 return mData.size(); 32 } 33 34 @Override 35 public String getItem(int position) { 36 return mData.get(position); 37 } 38 39 @Override 40 public long getItemId(int position) { 41 return position; 42 } 43 44 @Override 45 public View getView(int position, View convertView, ViewGroup parent) { 46 System.out.println("getView " + position + " " + convertView); 47 ViewHolder holder = null; 48 if (convertView == null) { 49 convertView = mInflater.inflate(R.layout.item1, null); 50 holder = new ViewHolder(); 51 holder.textView = (TextView)convertView.findViewById(R.id.text); 52 convertView.setTag(holder); 53 } else { 54 holder = (ViewHolder)convertView.getTag(); 55 } 56 holder.textView.setText(mData.get(position)); 57 return convertView; 58 } 59 60 } 61 62 public static class ViewHolder { 63 public TextView textView; 64 } 65 }
android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局,但当listview有大量的数据需要加载的时候,会占据大量内存,影响性能,这时候就需要按需填充并重新使用view来减少对象的创建。
ListView加载数据都是在public View getView(int position, View convertView, ViewGroup parent) {}方法中进行的(要自定义listview都需要重写listadapter:如BaseAdapter,SimpleAdapter,CursorAdapter的等的getvView方法),优化listview的加载速度就要让convertView匹配列表类型,并最大程度上的重新使用convertView。
getview的加载方法一般有以下三种种方式:
最慢的加载方式是每一次都重新定义一个View载入布局,再加载数据
public View getView(int position, View convertView, ViewGroup parent) {
View item = mInflater.inflate(R.layout.list_item_icon_text, null);
((TextView) item.findViewById(R.id.text)).setText(DATA[position]);
((ImageView) item.findViewById(R.id.icon)).setImageBitmap(
(position & 1) == 1 ? mIcon1 : mIcon2);
return item;
}
正确的加载方式是当convertView不为空的时候直接重新使用convertView从而减少了很多不必要的View的创建,然后加载数据
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, parent, false);
}
((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]);
((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap(
(position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
最快的方式是定义一个ViewHolder,将convetView的tag设置为ViewHolder,不为空时重新使用即可
static class ViewHolder {
TextView text;
ImageView icon;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text,
parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}