2

I used this SO question as a guide, but when I click a row it doesn't stay highlighted. What is wrong with my code?

score_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/score_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/keyboard"
        android:listSelector="@drawable/selector"
        android:choiceMode="singleChoice"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp" />
...
</RelativeLayout>

drawable/selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/highlight"/>
    <item android:state_pressed="true" android:drawable="@color/highlight"/>
</selector>

score_row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scoreRowLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/row_selector" >
...
</LinearLayout>

drawable/row_selector.xml

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

This code now works properly.

Community
  • 1
  • 1
GrilledCheese
  • 311
  • 3
  • 8
  • 13

1 Answers1

8

Try to use state_activated for your row_selector.

sstn
  • 3,050
  • 19
  • 32
  • 2
    In case anyone is wondering, you can also clear all choices with listview.clearChoices(); I have to use this unselect all rows. – GrilledCheese Feb 14 '13 at 05:47