ViewHolder本质上是一个静态类实例,你在创建视图时将其与视图关联,从而在运行时将要查找的子视图缓存起来。如果视图已经存在,请检索holder实例并使用其字段而不是调用findViewById。
在你的情况下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder; // to reference the child views for later actions
if (v == null) {
LayoutInflater vi =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
// cache view fields into the holder
holder = new ViewHolder();
holder.nameText = (TextView) v.findViewById(R.id.nameText);
holder.priceText = (TextView) v.findViewById(R.id.priceText);
holder.changeText = (TextView) v.findViewById(R.id.changeText);
// associate the holder with the view for later lookup
v.setTag(holder);
}
else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) v.getTag();
}
// no local variables with findViewById here
// use holder.nameText where you were
// using the local variable nameText before
return v;
}
// somewhere else in your class de@R_403_1292@n
static class ViewHolder {
TextView nameText;
TextView priceText;
TextView changeText;
}
警告:我没有尝试编译它.