I have a layout with two fragments. The left hand fragment is a ListFragment using a SimpleCursorAdaptor, the right hand one is populated with details about the item selected from the list in the left fragment. I'm trying to figure out how to make sure the selected item from the ListFragment stays highlighted until another item in the list is selected.
After some research I got as far as trying to use android:background="@drawable/item_selector"
I can changes the colors for the different states, but none of them seem to persist. I thought that selected would... it just seems logical that your selected item remains the selected item until you select a different one.
I even tried using v.setSelected(true);
in my clickhandler hoping it might maintain the state, but that didn't work either.
Is there a state I'm missing? I looked through the dev docs and nothing else seemed appropriate...
item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/green" />
<item
android:state_selected="true"
android:drawable="@color/blue" />
</selector>
I'm not sure what other code might help, so feel free to ask for whatever else you might think necessary.
The answer was in the android documentation after further research (two parts of the documentation really).
First, in touch mode there is no selected or focused state.
Second, by default, listviews are set to a choice mode of none (meaning that no item in the list can have a chosen state). All I had to do was change the choice mode (it could be single or multiple, I only needed/wanted single) by adding this:
Then I used the choice state in my selector XML (translates to the
activated
state):Apply that in the row layout xml file as the background:
My chosen item now appears with a blue background until a different item is chosen.
Note that this (android:state_activated) requires Android API 11 or later.
I believe you need to add state_focused too to make it feel like selected or highlighted.
This worked very nice for me in the
ListFragment
, but I think it works only for Android 4.0 and up. I created a simple layout for the list item withandroid:background="?android:attr/activatedBackgroundIndicator
(in case of Android below 4.0 you need to ceate the similar one in drawables):And then, just create a simple
ArrayAdapter
using this layout and set the choice mode to single:After that you can hightlight the necessary item with
setItemChecked
method: