0

I'm using SherlockListFragment to show list items, and I want to set the background of list item highlighted when it is checked. But after I did all the things below, the list item background still remain unchanged when set checked (but it changes when clicked). Why?

The list selector => drawable/list_selector.xml:

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

    <item android:state_window_focused="false"
        android:drawable="@color/transparent" />

    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_dark" />

    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/list_selector_background_dark" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_dark" />

    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_dark" />

    <item android:state_checked="true"
        android:drawable="@drawable/list_selector_background_dark" />
</selector>

The list item layout => layout/list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dip"
    android:background="@drawable/list_selector_dark">

    ......

</RelativeLayout>

The fragment layout => layout/fragment_list.xml:

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

    <ListView 
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_gravity="start"
        android:scrollbars="none"
        android:fastScrollEnabled="true"
        android:choiceMode="singleChoice"
        android:listSelector="@drawable/list_selector_dark" />

    <TextView 
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/no_entry" />

</LinearLayout>
teejoe
  • 673
  • 2
  • 6
  • 17
  • There are already questions like these: http://stackoverflow.com/questions/14475014/listfragment-selected-item , http://stackoverflow.com/questions/12130266/highlight-selected-item-in-listfragment, http://stackoverflow.com/questions/12742269/android-set-list-view-item-as-selected-highlighted – carlosmaciel Mar 17 '14 at 16:04
  • thank you, I've tried many answers but they still don't work for me. I'll try these one again. – teejoe Mar 18 '14 at 01:37

0 Answers0