Reputation: 1659
I am working on a ListView
and I have used setBackgroundColor
inside onItemLongClickListener
on the selected item. My problem is that when I am doing this and scrolling, it is setting color of some invisible child of ListView
too. How can it be solved.
Upvotes: 2
Views: 952
Reputation: 274
Try putting the following attributes in your xml:
`
<ListView
android:dividerHeight="1dp"
android:scrollingCache="false" >
`
Upvotes: 2
Reputation: 1997
In your adapter class:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(...);
}
convertView.setBackgroundColor(defaultcolor);
...
}
This however will overwrite the background you set in the onlongclicklistener when that view would be redrawn. So you might want to keep a list of the positions of the clicked items so you can set these in the getView method.
Upvotes: 0
Reputation: 116332
this is caused since a listview uses old views in order to avoid re-creation of views when you scroll.
in fact , this is common to all of the adapterView classes .
in order to handle this , store the status of the position of the view (using an arrayList or whatever collection you wish) and on the getView , if the position is set in the list to be of this background , use this background , otherwise use the default background.
for more information about listview , either read the API , or (and i highly recommend it) watch the video "the world of listView" .
Upvotes: 1