ListView reusing views when ... I don't want it to

Viewed 45502

I've got a ListView, each of item of which contains a ToggleButton. After I toggle it and then scroll up or down, the ListView is recycling the Views and so some of the others are mirroring the checked state of the ToggleButton. I don't want this. How can I prevent it?

5 Answers

This would make it so slow for large lists. But inside getView(), you can use:

if (listItemView == null || ((int)listItemView.getTag()!=position)) {
            listItemView = LayoutInflater.from(context).inflate(R.layout.edit_text_list_item,
                    parent, false);
}

listItemView.setTag(position);
// set inner Views data from ArrayList
...

The tag is an Object that is associated with the View. And you check whenever you recycle it if you can recycle it or not. This makes each list item be inflated and nothing will be recycled.

This also should prevent deleting text from EditText inside the ListView and also prevent images from being reordered or messed up if your ListView has images in it.

Related