LiseView中使用ViewCache,


在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;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值