当View加载或者回收时候都会通过findViewById()去查找View,假如分层很多,很耗时,这时采用ViewHolder,使用时候直接去取,不需要查找。
具体代码:
//在你自己的游标类里
public static class ViewHolder {
public final ImageView iconView;
public final TextView dateView;
public final TextView descriptionView;
public final TextView highTempView;
public final TextView lowTempView;
public ViewHolder(View view) {
iconView = (ImageView)view.findViewById(R.id.list_item_icon);
dateView = (TextView)view.findViewById(R.id.list_item_date_textview);
descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
highTempView = (TextView) view.findViewById(R.id.list_item_high_textview);
lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview);
}
}
//在newView方法里
View view = LayoutInflater.from(context).inflate(layoutId,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
//在bindView方法里
//使用viewHolder来访问View
ViewHolder viewHolder = (ViewHolder) view.getTag(); viewHolder.iconView.setImageResource(R.drawable.ic_launcher);