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.