Вопрос

The following problem is driving me crazy. I've defined an xml fragment, using this code:

<fragment class="host.helperclass.ServiceListFragment"
        android:id="@+id/titles"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="0px"/>

Then I've implemented ServiceListFragment class which extends SherlockListFragment.

I've overridden the onActivityCreated method, with this code:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setDividerHeight(2);
getListView().setBackgroundColor(Color.parseColor("#EEEEEE"));
getListView().setCacheColorHint(Color.parseColor("#00000000"));
getListView().setSelector(R.drawable.list_selector);

Where list_selector.xml is very simple:

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

But the green background for an item is added only when I press the button. When I release it the green background is removed. It seems like the state_selected is not recognized. Is this possible? If so, is there any way for enable the selection of items inside ListView?

NOTE:

I've also tried to use a selctor with only one item:

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

And as result I obtaint that, when I press the ListView item the background become green, but when I release it the green background is removed. It seems that my item lost focus when I pull off my finger from item.

EDIT:

I have made another attempt.

I've added inside the onListItemClick method this line of code:

getListView().setItemChecked(index, true);

and in my xml selector I have put only this item:

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

But strangely this does not work.

Это было полезно?

Решение 3

I found the problem.

Defining the xml layout for the row used in the custom adapter, I needed to add this row, to the root element:

android:background="?android:attr/activatedBackgroundIndicator"

But, the attribute activatedBackgroundIndicator has been introduced since API level 11, so I have put the previous version of my xml in layout folder, and the new one in the layout-v11 folder.

Also the following statement is required, inside the onListItemClick method:

getListView().setItemChecked(position, true);

Другие советы

I believe the correct state for single choice mode is android:state_activated.

I noticed you are using the selector for the list.

You should use it as the background of the list row (the one you use in the adapter).

For example:

row_background.xml

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

row_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/row_background">

    <!-- your views -->

</LinearLayout>

Try this.

Add this item to selector

<item android:state_selected="true"
      android:state_pressed="false"
      android:drawable="@drawable/green" />

and remove

<item android:state_selected="true"
      android:drawable="@drawable/green"/>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top