View selection was deselected while scrolling in listview
I want to change background color of selected item in listview.
When I click on list item then I get the view in:
onItemClick(AdapterView<?> arg0, View view, int position, long arg3)开发者_Python百科
and then changed the background of that view like this:
view.setBackgroundColor(Color.BLUE);
It's working fine when it was clicked. The background was changed. But I have one problem.
For example I am having 10 items in listview at first only 5 items visible (because of screen resolution) if I am scrolling I can visible the next 5 items.
At first if I am going to select 1,3 items there background color is changing then I am scrolling to next five if again I am coming to back I can't see the 1,3 as clicked (their background color is changing to original color).
How can I solve this problem?
Your listAdapter reuses the views while scrolling, therefore the view which was affected might no longer be the same view as before the scrolling began. You should instead add an ivar to the model, and change it to indicate that object has been clicked and now has a different background color.
精彩评论