android ListView won't highlight selected row

梦想的初衷 提交于 2019-12-05 14:56:30

Create a selector for the list view in your drawables folder and set the list item background to this drawable. See the code below

select_button_background.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/select_button_selected" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/select_button_selected" android:state_enabled="true" android:state_selected="true"/>

    <item android:drawable="@drawable/select_button_selectable" android:state_pressed="false"/>
    <item android:drawable="@drawable/select_button_selectable" android:state_selected="false"/>
 </selector>

select_button_selectable.xml

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

    <gradient
        android:angle="270"
        android:endColor="#CCCCCC"
        android:startColor="#DDDDDD"
        android:type="linear" />

    <corners android:radius="10dp" />

    <shape>
        <stroke
            android:width="1dp"
            android:color="#333333" />
    </shape>

</shape>

select_button_selected.xml

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

    <gradient
        android:angle="270"
        android:endColor="#AAAAAA"
        android:startColor="#BBBBBB"
        android:type="linear" />

    <corners android:radius="10dp" />

    <shape>
        <stroke
            android:width="1dp"
            android:color="#333333" />
    </shape>

</shape>
Nickolaus

you have changed your ListSelector: android:listSelector="@drawable/list_selected" I assume your custom drawable is not a selector, which has different states. See this site or this question to understand the usage

Pragnani

Hi I have already posted the answer for you in this question link android - identify items in a ListView, you said it hasn't worked for you, I have provided the same answer and it worked for other check this question Inflate ListView row from OnClickListener in Android?. I hope this will help you.

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