public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder.text = (TextView) convertView.findViewById( R.id.text));
holder.icon = (ImageView) convertView.findViewButId( R.id.icon));
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[pos]);
holder.icon.setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
// holder = new ViewHolder();
}
// ViewHolder 模式, 效率提高 50%
static class ViewHolder {
TextView text;
ImageView icon;
}
// 内存分配 不要创建 Java 对象 在性能敏感的代码里, 尽量避免创建 Java 对象
测量: 布局: onMeasure() onLayout() 绘图:
事件处理: dispatchTouchEvent(), onTouchEvent()
Adapter: getView(), bindView()
GC, 垃圾回收
整个程序会暂停 慢 (大约几百个毫秒)
本文介绍了一种通过ViewHolder模式来优化Android中ListView性能的方法。ViewHolder模式可以显著减少findViewById的调用次数,从而提升应用性能。文章详细解释了如何实现ViewHolder,并展示了如何在getView方法中使用该模式。
1万+

被折叠的 条评论
为什么被折叠?



