在listView中使用BaseAdapter时,可以设置ViewCache对象,在此访问ListView时可以直接从缓存中加载数据,代码如下
private class MyAdapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView nameView = null;
TextView phoneView = null;
TextView amountView = null;
if(convertView == null){//只有第一页显示会new出来item对象,后边的显示会使用此模板
convertView = inflater.inflater(resource,null);
nameView = convertView.findViewById(R.id.name);
phoneView = convertView.findViewById(R.id.phone);
amountView = convertView.findViewById(R.id.amount);
ViewCache cache = new ViewCache();//视图缓存器
cache.nameView = nameView;
cache.phoneView = phoneView;
cache.amountView = amountView;
convertView.setTag(cache);//为此视图设置缓存标签
}else{//在此访问此页面直接使用缓存数据
ViewCache cache = (ViewCache)convertView.getTag();
nameView = cache.nameView;
phoneView = cache.phoneView;
amountView = cache.amountView;
}
Person person = persons.get(position);
//下面的代码实现数据绑定
nameView.setText(person.getNmae());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount());
return convertView;
}
}
private fianl class ViewCache{
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}