android view holder 优化

android 一般都用viewholder来优化contentView,采用sparseArray能够进一步优化

/**
* 用法: ImageView bananaView = ViewHolder.get(convertView, R.id.banana);
*
* @param convertView
* @param id
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends View> T getView(View convertView, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
convertView.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = convertView.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}

  

上一篇:Linux常用命令_(系统管理)


下一篇:NSURL基本操作示例说明