Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView Selector not working with ListViewItem's background set

I have a ListView that has a selector set with just a solid color.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/assignmentSelectedItem" />
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/assignmentsListView"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:listSelector="@drawable/assignment_list_selector" />
</LinearLayout>

assignment_list_selector.xml -

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

In the ListView.Adapter for my list, I have items that have their view's background set to a specific color. My question would be is that why the selector is not working at all in displaying a selected color for my list view? If so, any suggestion of a work around with allowing me to still set the view's background color.

like image 918
Bryan Avatar asked Oct 19 '12 18:10

Bryan


1 Answers

You can use ListView property android:drawSelectorOnTop="true" in your layout .xml file in order to made list view to draw the selector over the list item rather than under them.

If you wonna have item's background + list selector functionality you can try another approach: Use transparent list selector. And modify your selector in order to use as a background for the list item. For example

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@android:color/white" />
    <item android:drawable="@android:color/your_list_item_color"/>
</selector>
like image 85
rus1f1kat0R Avatar answered Sep 21 '22 22:09

rus1f1kat0R