static
class
ViewHolder {
public
static
<T
extends
View> T get(View view,
int
id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if
(viewHolder ==
null
) {
viewHolder =
new
SparseArray<View>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if
(childView ==
null
) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return
(T) childView;
}
}
/**
*这是工具类的实现,稍微说下实现的原理:
*
*1、ViewHolder既然是依赖View的Tag存放,但是以一个 SparseArray 集合存放。
*
*2、判断View里的Tag是否存在viewHolder,不存在,赶紧叫她生一个。
*
*3、然后在viewholder(也就是SparseArray)寻找View的索引,如果没有,赶紧findViewById一个
*put进去顺便return出来,如果已经存在,皆大欢喜,直接用呗。
*
*
*/
/**
*贴个BaseAdapter里面使用的代码:
*/
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
if
(convertView ==
null
) {
convertView = inflater.inflate(R.layout.listview_item_layout, parent,
false
);
}
TextView name = Tools.ViewHolder.get(convertView, R.id.student_name);
TextView age =Tools.ViewHolder.get(convertView, R.id.student_age);
Student data = (Student) getItem(position);
name.setText(data.getName());
age.setText(data.getAge());
return
convertView;
}