How do I apply layout changes to one individual ListView item without the adapter recycling the layout to other items?

125 views Asked by At

I have a custom adapter for a ListView. On getView(), I attach an onTouchListener to the convertView.

Upon touch of the item/convertView, I update the ViewHolder's RelativeLayout's LayoutParams to be shifted left and set a couple buttons revealed underneath the uppermost layout to clickable, as described on a few swipeable ListView guides.

Unfortunately, when I scroll down my ListView, other items also now have the layout slid to the side with the buttons set clickable underneath. I am not sure why a change to a single ViewHolder is being recycled to others, but I'm sure I'm just falling prey to a simple misunderstanding. Can anyone offer an explanation and solution? Thanks!

2

There are 2 answers

0
Marcos Vasconcelos On

After recycling a view you should reset the view to its initial state and populate it with the new values.

0
NehaK On

You can use an simple Array of booleans for this. for example,

List has 5 items so initially array will be-

arr[false,false,false,false,false]

When you select any item then update arrays value at that items positions-

list item 1 selected - arr [true,false,false,false,false]

list item 3 selected - arr [true,false,true,false,false]

list item 5 selected - arr [true,false,true,false,true]

and check array's values in getView's method-

if(arr[position] == false)
{
   // item not selected so do changes you want in non-selected items 
}
else
{
   // item selected so do changes you want in selected items 
}