T
T
TimkaTV2015-08-05 01:10:32
Android
TimkaTV, 2015-08-05 01:10:32

How to properly use ViewHolder + LisView?

Good afternoon,
I used the ViewHolder pattern:

public View getView(int position, View convertView, ViewGroup parent) {
        switch (type) {
            case 0: return item_view_new(data, position, convertView, parent);
            case 1: return item_view_new(data, position, convertView, parent);
            case 2: return item_view_new(data, position, convertView, parent);
            case 3: return item_view_video(data, position);
            case 4: return item_view_video(data, position);
            case 5: return item_view_video(data, position);
            case 6: return item_view_meeting(data, position);
            case 10: return item_view_new_error(data, position);
            case 99: return item_view_new(data, position, convertView, parent);
        }
        return null;
    }



    public View item_view_new(ArrayList<HashMap<Integer, Object>> data, int position,final View convertView, View parent) {
        View v = convertView;
        ViewHolder viewHolder;
        if (v == null) {
            viewHolder = new ViewHolder();

            v = LayoutInflater.from(context).inflate(R.layout.new_layout_item, (ViewGroup) parent, false);

            viewHolder.text_group = (TextView) v.findViewById(R.id.group);
            viewHolder.text_date = (TextView) v.findViewById(R.id.date);
            viewHolder.title = (TextView) v.findViewById(R.id.title);
            viewHolder.text = (TextView) v.findViewById(R.id.content);
            viewHolder.author = (TextView) v.findViewById(R.id.author);
            viewHolder.addition = (TextView) v.findViewById(R.id.addition);
            viewHolder.img = (ImageView) v.findViewById(R.id.image);
            viewHolder.ico_group = (ImageView) v.findViewById(R.id.ico_group);

            viewHolder.text_group.setTypeface(Typeface.createFromAsset
                    (context.getAssets(), "Ubuntu-M.ttf"));
            viewHolder.text_date.setTypeface(Typeface.createFromAsset
                    (context.getAssets(), "Ubuntu-N.ttf"));
            viewHolder.title.setTypeface(Typeface.createFromAsset
                    (context.getAssets(), "Ubuntu-M.ttf"));
            viewHolder.text.setTypeface(Typeface.createFromAsset
                    (context.getAssets(), "Ubuntu-L.ttf"));
            viewHolder.author.setTypeface(Typeface.createFromAsset
                    (context.getAssets(), "Ubuntu-N.ttf"));
            viewHolder.addition.setTypeface(Typeface.createFromAsset
                    (context.getAssets(), "Ubuntu-N.ttf"));
            viewHolder.text.setTextSize(Integer.parseInt(sharedPref.getString("size_preview", "15")));
            viewHolder.pos = position;
            v.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) v.getTag();

        }

        viewHolder.text_group.setText(themetext(data.get(position).get(NAMEGROUP).toString()));
        viewHolder.text_date.setText(data.get(position).get(DATE).toString());
        viewHolder.title.setText(data.get(position).get(HEADER).toString());
        viewHolder.text.setText(ExtraClass.text_clear(data.get(position).get(CONTENT).toString()));
        viewHolder.author.setText(data.get(position).get(AUTHOR).toString());
        ExtraClass.setIco(data.get(position).get(NAMEGROUP).toString(), viewHolder.ico_group, context);
        return v;
    }


class ViewHolder {
    static TextView text_group;
    static TextView text_date;
    static TextView title;
    static TextView text;
    static TextView author;
    static TextView addition;
    static ImageView img;
    static ImageView ico_group;
    static int pos;
}

The scrolling is really smooth, but only 3-4 records are loaded, and then they start to duplicate (moreover, if you play with the call, it sometimes draws the correct records) Maybe someone made similar mistakes? Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Эмин, 2015-08-05
@TimkaTV

v = LayoutInflater.from(context).inflate(R.layout.new_layout_item, (ViewGroup) parent, false);
- тут не нужно parentкастовать в ViewGroup
Проблема тут:
class ViewHolder {
    static TextView text_group;
    static TextView text_date;
    static TextView title;
    static TextView text;
    static TextView author;
    static TextView addition;
    static ImageView img;
    static ImageView ico_group;
    static int pos;
}

Все поля статические, а значит их значения общие для всех вьюхолдеров. Как можно было такую ошибку допустить?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question