I have a simple ListView
and I want each of it items to be highlighted on user\'s touch. I thought this should happen by default but it isn\'t. Can you advice?
I struggled with this for a few days. In the end, I have to create a widget that supports Checkable
interface and return that widget/view in my adapter's getiew()
function. And the listview needs to be in ListView.CHOICE_MODE_SINGLE
(or possibly any other mode specified by android:choiceMode
) for it to keep track of the choice made on the UI.
So in essence, all the following needs to be in place for the listview item to stay highlighted:
ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ListAdapter.getView()
return a view that implements Checkable
interfaceListView.OnItemClickListener
should call setItemChecked(position, true)
to mark the item to be checked (and thus highlight it in the listview)Hope this can help someone who is also struggling with this.