One of the activities I'm building contains a ListView
, and a GridView
. The contents of the gridview will be updated based on the item selected in the ListView. Now, I'm using a selector to highlight the selected item in ListView by changing its background. But once the ListView loses focus, the selected item is not highlighted anymore. Is there a way to have the list item highlighted even after the focus is lost by the list view?
Asked
Active
Viewed 2,548 times
3

Gunanaresh
- 959
- 1
- 9
- 20
-
I got partial success using `choiceMode="singleChoice"`. And selector did work well when I used CheckedTextView for choosing the background. But I face some issues when I use the custom CheckedLinearLayout as the layout for each item. I shall update this thread once I fix it. – Gunanaresh Oct 20 '11 at 08:20
2 Answers
1
Set your selector to recognize the activated state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:state_selected="true"
android:drawable="@android:color/holo_blue_light" />
<item android:state_activated="true"
android:drawable="@android:color/holo_blue_light" />
<!-- Default Item -->
<item android:state_selected="false"
android:drawable="@android:color/white" />
</selector>
And not only select, but activate it onListItemClick
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
view.setSelected(true);
view.setActivated(true);
}

Machado
- 14,105
- 13
- 56
- 97
0
I think you want to maintain the state of the clicked Item of some List. For this you can use a boolean ArrayList to maintain the state for every row of list. And you can use this ArrayList in the getView() of your Adapter. Set the background depending on the state. e.g. Set the background image or color of the View in getView() . Here is an idea that I am talking about. You can use it for your purpose as well.