Android中Listview展示及其优化好处

时间:2021-10-22 23:50:59

展示效果:

Android中Listview展示及其优化好处

中间的item条目是可以上下滑动的。

代码实现:

     @Override
public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(CleanCacheActivity.this,
R.layout.list_item_cacheinfo, null);
holder = new ViewHolder(); holder.tvName = (TextView) convertView
.findViewById(R.id.tv_name);
holder.tvCache = (TextView) convertView
.findViewById(R.id.tv_cache);
holder.ivIcon = (ImageView) convertView
.findViewById(R.id.iv_icon);
holder.ivClean = (ImageView) convertView
.findViewById(R.id.iv_clean); convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final CacheInfo info = getItem(position);
holder.tvName.setText(info.name);
holder.ivIcon.setImageDrawable(info.icon);
26        //......
27         return convertView;
28     }
     // listview 属性封装
class ViewHolder {
public TextView tvName;
public TextView tvCache;
public ImageView ivIcon;
public ImageView ivClean;
} // 缓存信息封装
class CacheInfo {
public String name;
public String packageName;
public Drawable icon;
public long cacheSize;
}

优化特点:

1.

  减少findViewById的次数

2.

  减少创建对象的次数(holder = new ViewHolder();)

3.

  实现分页加载,节省消耗。