How to highlight ListView item on touch?

后端 未结 4 1563
野的像风
野的像风 2021-02-02 17:46

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?

4条回答
  •  隐瞒了意图╮
    2021-02-02 18:33

    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:

    1. ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    2. ListAdapter.getView() return a view that implements Checkable interface
    3. ListView.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.

提交回复
热议问题