Android Listview Setselection() Does Not Seem to Work

setSelected() works buggy with ListView

For API 11+:

1) Set list view to single choice:

<ListView
android:choiceMode="singleChoice" />

2) Set the background of the root element of your item layout to your selector:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/selector">

3) Change android:state_selected to android:state_activated:

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

4) Highlight item using listView.setItemChecked(index, true);

Note: To make it clear regarding view's states, specially state_activated you can check this post; it is interesting.

How to set selected item of ListView without scrolling the view [Android]

I think it's hardcoded and thus not possible without scrolling



Related Topics



Leave a reply



Submit