展示效果:
中间的item条目是可以上下滑动的。
代码实现:
1 @Override 2 public View getView(int position, View convertView, ViewGroup parent) { 3 4 ViewHolder holder; 5 if (convertView == null) { 6 convertView = View.inflate(CleanCacheActivity.this, 7 R.layout.list_item_cacheinfo, null); 8 holder = new ViewHolder(); 9 10 holder.tvName = (TextView) convertView 11 .findViewById(R.id.tv_name); 12 holder.tvCache = (TextView) convertView 13 .findViewById(R.id.tv_cache); 14 holder.ivIcon = (ImageView) convertView 15 .findViewById(R.id.iv_icon); 16 holder.ivClean = (ImageView) convertView 17 .findViewById(R.id.iv_clean); 18 19 convertView.setTag(holder); 20 } else { 21 holder = (ViewHolder) convertView.getTag(); 22 } 23 final CacheInfo info = getItem(position); 24 holder.tvName.setText(info.name); 25 holder.ivIcon.setImageDrawable(info.icon);26 //......27 return convertView;28 }
1 // listview 属性封装 2 class ViewHolder { 3 public TextView tvName; 4 public TextView tvCache; 5 public ImageView ivIcon; 6 public ImageView ivClean; 7 } 8 9 // 缓存信息封装 10 class CacheInfo { 11 public String name; 12 public String packageName; 13 public Drawable icon; 14 public long cacheSize; 15 }
优化特点:
1.
减少findViewById的次数
2.
减少创建对象的次数(holder = new ViewHolder();)
3.
实现分页加载,节省消耗。
时间: 2024-10-12 06:21:44