3

After going though all the related questions and talking to some expert developers I still can't figure this one out.

My problem is, that when a group item in an expandable list view is selected using setSelected from the adapter, it's background does not change color using the drawable selector. I had the exact same problem with a regular list view and a single text view.

My adapter inflates the item using the xml below.

My activity layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <RelativeLayout
        ...
        android:id="@+id/undo"
        ...
        >
    ...... (irrelevant)
    </RelativeLayout>

    <ExpandableListView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/main_view"
        android:layout_gravity="left|center_vertical"
        android:paddingTop="4dip"
        android:paddingBottom="4dip"
        android:paddingLeft="?android:listPreferredItemPaddingLeft"
        android:paddingRight="?android:listPreferredItemPaddingRight"
        android:layout_below="@id/undo"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_margin="4dp"
        />

</RelativeLayout>

My group list item:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:minHeight="?android:listPreferredItemHeight"
    android:background="@drawable/main_group_selector"
    >

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/switch1"
        android:layout_gravity="center_horizontal|top"
        android:checked="true"
        android:tag="0"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/text1"
        android:gravity="center_vertical"
        android:textAlignment="gravity"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/switch1"
        android:paddingRight="4dip"
        />

</RelativeLayout>

My selector @drawable/main_group_selector :

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
    <item android:state_selected="false" android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/list_focused_minitasker" />
    <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/pressed_background_minitasker" />
    <item android:state_selected="false" android:drawable="@android:color/transparent" />
    <item android:state_selected="true" android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/list_focused_minitasker" />
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/pressed_background_minitasker" />
    <item android:state_selected="true" android:drawable="@android:color/holo_red_dark" />
</selector>

Note I added android:exitFadeDuration to the selector, which makes the background of the list items i want to flash red momentarily. When I refresh the whole list: all of the selected items flash red, so i know that setselected works as intended.

So my problem, now is that they don't stay red (the background becomes transparent).

I've tried changing order, using other permutations and configuration settings without success.

I've also tried using listselector, which didn't work and the google expert I talked to said it was not the right way anyway.

Divya Jain
  • 393
  • 1
  • 6
  • 22
Anton Wolkov
  • 163
  • 2
  • 10
  • try to set a Background on your root `RelativeLayout` (I mean the List's background). That should solve your problem – Rafael T Nov 04 '13 at 15:42
  • added android:background="@android:color/transparent" to ExpandableListView and it's RelativeLayout, no change. – Anton Wolkov Nov 04 '13 at 16:04

0 Answers0