In my Android project I have a ListView with this item layout (it's the simple_list_item_1 layout with the new property android:background)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@drawable/simple_listselector"
/>

My android:background="@drawable/simple_listselector" looks like this:

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

But if I now call setSelected(true) for one of the listItems the selector is not visible. Do I have to change somthing in the selector or the item layout?

有帮助吗?

解决方案

First of all you need to set the selection mode of a ListView

ListView lv=new ListView(this) or findViewById(R.id.my_list_view);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

and here is your xml file for a background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/state_selected" />
<item android:drawable="@android:drawable/state_unselected" />
</selector>

here you go

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top