逗比式、文艺式、普通式
(1)逗比式
View view=mInflayer.inflate(R.layout.item, null);
存在缺点:重复创造View 、.findViewById依然会浪费大量时间
//逗比式-----------------
View view=mInflayer.inflate(R.layout.item, null);//mIfalter 将xml布局文件转化为view
ImageView imageView =(ImageView) view.findViewById(R.id.iv_image);
TextView title=(TextView) view.findViewById(R.id.tv_title);
TextView content=(TextView) view.findViewById(R.id.tv_content);
ItemBean bean=mList.get(position);
imageView.setImageResource(bean.ItemImageResid);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return view;
(2)普通式
充分利用了ListView的缓存特性,如果没有缓存才创建新的View
if(convertView==null){ //为空,View 未被实例化,缓存池中没有缓存
convertView=mInflayer.inflate(R.layout.item, null);
}
存在缺点:.findViewById依然会浪费大量时间
//普通式-----------------
if(convertView==null){ //为空,View 未被实例化,缓存池中没有缓存
convertView=mInflayer.inflate(R.layout.item, null);
}
ImageView imageView =(ImageView) convertView.findViewById(R.id.iv_image);
TextView title=(TextView) convertView.findViewById(R.id.tv_title);
TextView content=(TextView) convertView.findViewById(R.id.tv_content);
ItemBean bean=mList.get(position);
imageView.setImageResource(bean.ItemImageResid);
title.setText(bean.ItemTitle);
content.setText(bean.ItemContent);
return convertView;
(3)文艺式
使用ViewHolder 实现显示数据的视图的缓存
//文艺式-----------------
ViewHolder viewHolder;
if(convertView==null){ //为空,View 未被实例化,缓存池中没有缓存
viewHolder=new ViewHolder();
convertView=mInflayer.inflate(R.layout.item, null);
viewHolder.ImageView=(ImageView) convertView.findViewById(R.id.iv_image);
viewHolder.title=(TextView) convertView.findViewById(R.id.tv_title);
viewHolder.content=(TextView) convertView.findViewById(R.id.tv_content);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
ItemBean bean=mList.get(position);
viewHolder.ImageView.setImageResource(bean.ItemImageResid);
viewHolder.title.setText(bean.ItemTitle);
viewHolder.content.setText(bean.ItemContent);
return convertView;
}
class ViewHolder{
public ImageView ImageView;
public TextView title;
public TextView content;
}