Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight mysterious behaviour in ListView

I have set a custom list selector for a Simple ListView but when I select an item, the whole ListView turns blue. I don't understand where the problem is.

This is my ListView:

<ListView android:id="@+id/TopListView" android:layout_width="fill_parent"
    android:listSelector="@drawable/regular_selector"
    android:layout_height="wrap_content">
</ListView>

and it's the regular_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@color/transparent" />
  <item android:state_pressed="true"
        android:drawable="@color/blue" />
  <item android:state_focused="true"
        android:drawable="@color/blue" />
</selector>
like image 391
Maxime Avatar asked Dec 13 '11 17:12

Maxime


1 Answers

I was having the same issue of the whole list view highlighting, when using a color as the background. Curiously this only happened below api 11.

Solution was to use a solid shape drawable to wrap the colour:

list_selector_shaped_background_press.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/list_selector_pressed"/>
</shape>

List_selector_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

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

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/list_selector_background_disabled" />

    <!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects -->
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_shaped_background_pressed" />

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

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

</selector>
like image 198
scottyab Avatar answered Sep 18 '22 20:09

scottyab