Android: set list view item as “selected” (highlighted)

℡╲_俬逩灬. 提交于 2019-11-28 05:33:14

OK, finally got my answer.

The idea is to use the state_activated in the selector and

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE) 

in the code, or

android:choiceMode="singleChoice"

in the xml, of course

This is how the selector should look like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/solid_white" android:state_activated="false"/>
    <item android:drawable="@drawable/solid_white" android:state_activated="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/listitem_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/listitem_focused" android:state_activated="true"/>

</selector>

This is how the list item layout should be:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem_background"
    android:orientation="vertical" >
...
<LinearLayout/>
Joan P.S

I faced the same issue and then I just needed a simple line in my item view xml.

android:background="?android:attr/activatedBackgroundIndicator"

This post could help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!