LiseView中使用ViewCache,

来源:互联网 发布:阿里云远程连接不上 编辑:程序博客网 时间:2024/06/05 16:03


在listView中使用BaseAdapter时,可以设置ViewCache对象,在此访问ListView时可以直接从缓存中加载数据,代码如下

private class MyAdapter extends BaseAdapter{public int getCount() {// TODO Auto-generated method stubreturn 10;}public Object getItem(int position) {// TODO Auto-generated method stubreturn null;}public long getItemId(int position) {// TODO Auto-generated method stubreturn 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;}


	
				
		
原创粉丝点击