数据适配器——BaseAdapter
可以自定义View或者利用参数中的convertView
没有任何优化处理,每次都创建新的View,设置控件,效率极其低下
``` @Override
public View getView(int position, View convertView, ViewGroup parent) {
//逗比式(没有利用到ListView的缓存机制)//
View view = mInflater.inflate(R.layout.item,null);//装载到item中的布局文件
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView title = (TextView) view.findViewById(R.id.iv_title);
TextView content = (TextView) view.findViewById(R.id.iv_content);
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImage);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return view;
//逗比式//////////////////////////
}
二、普通式
-----
利用了ListView的缓存特性,如果没有缓存才创建新的View。算入门,但是findViewById依然会浪费大量时间
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//普通式////////////////////
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
TextView title = (TextView) convertView.findViewById(R.id.iv_title);
TextView content = (TextView) convertView.findViewById(R.id.iv_content);
ItemBean bean = mList.get(position);
imageView.setImageResource(bean.ItemImage);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return content;
//普通式////////////////////
三、文艺式
-----
不仅利用了ListView的缓存,更通过ViewHolder类来实现显示数据的视图的缓存,避免多次通过findViewById寻找控件(这是最号的写法)
1>创建内部类ViewHolder
2>判断convertView是否为空
3>通过setTag将ViewHolder与convertView绑定
//文艺式///////////////////
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
viewHolder.title = (TextView) convertView.findViewById(R.id.iv_title);
viewHolder.content = (TextView) convertView.findViewById(R.id.iv_content);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
ItemBean bean = mList.get(position);
viewHolder.imageView.setImageResource(bean.ItemImage);
viewHolder.title.setText(bean.ItemTitle);
viewHolder.content.setText(bean.ItemContent);
return convertView;
//文艺式///////////////////
}
class ViewHolder {
public ImageView imageView;
public TextView title;
public TextView content;
}
热门评论
其实findviewbyid并不会优化太多,具体可以看谷歌的io大会的视频。但是能优化则优化,数据量越大,优势越明显