ViewHolder在自定义适配器中的实用程序

时间:2017-05-03 08:37:08

标签: android android-adapter android-viewholder

自定义ViewHolderAdapter的效用是什么?

我使用Adapter创建了一个ViewHolder而另一个没有,我有相同的结果......那么这个实用程序的用途是什么?

这是我的 CustomAdapter.class ,其中包含ViewHolder

public class CustomAdapter extends ArrayAdapter<Fruit> {

    public class ViewHolder {
        TextView txtName;
        TextView txtColor;
    }

    CustomAdapter(Context context, ArrayList<Fruit> data) {
        super(context, R.layout.item_fruit, data);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        final Fruit fruit = getItem(position);

        ViewHolder viewHolder = new ViewHolder();

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.item_fruit, parent, false);
        }

        viewHolder.txtName = (TextView) convertView.findViewById(R.id.textView_fruit_name);
        viewHolder.txtColor = (TextView) convertView.findViewById(R.id.textView_fruit_color);

        if (fruit != null) {
            viewHolder.txtName.setText(fruit.getName());
            viewHolder.txtColor.setText(fruit.getColor());
        }

        return convertView;
    }
}

这是我的 CustomAdapter.class ,没有ViewHolder

public class CustomAdapter extends ArrayAdapter<Fruit> {

    CustomAdapter(Context context, ArrayList<Fruit> data) {
        super(context, R.layout.item_fruit, data);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        final Fruit fruit = getItem(position);

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.item_fruit, parent, false);
        }

        TextView txtName = (TextView) convertView.findViewById(R.id.textView_fruit_name);
        TextView txtColor = (TextView) convertView.findViewById(R.id.textView_fruit_color);

        if (fruit != null) {
            txtName.setText(fruit.getName());
            txtColor.setText(fruit.getColor());
        }

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

要正确使用ViewHolder,您需要将代码更改为:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    final Fruit fruit = getItem(position);

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.item_fruit, parent, false);
        convertView.setTag(new ViewHolder(convertView));
    }

    ViewHolder viewHolder = (ViewHolder) convertView.getTag();

    viewHolder.txtName = (TextView) convertView.findViewById(R.id.textView_fruit_name);
    viewHolder.txtColor = (TextView) convertView.findViewById(R.id.textView_fruit_color);

    if (fruit != null) {
        viewHolder.txtName.setText(fruit.getName());
        viewHolder.txtColor.setText(fruit.getColor());
    }

    return convertView;
}

您的ViewHolder会像:

private class ViewHolder {
    public TextView txtName;
    public TextView txtColor;

    public ViewHolder(View view) {
        txtName = (TextView) view.findViewById(R.id.txt_name);
        txtColor = (TextView) view.findViewById(R.id.txt_color);
    }
}

拨打findViewById()费用很高,每次拨打getView()时在您的观看次数上调用它都会浪费很多时间。

使用ViewHolder的唯一好处是通过在重复使用视图时不调用findViewById()来节省时间,即当convertView不是null时。