您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使ArrayAdapter遵循ViewHolder模式?

如何使ArrayAdapter遵循ViewHolder模式?

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;
}

警告:我没有尝试编译它.

其他 2022/1/1 18:14:46 有619人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶