Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing the current selection in a listview

As seen in the tablet version of gmail and google talk I am trying to show the current selection in a listview. I know this is not standard practice and should be avoided when necessary.in my program the listview is alway on the screen and the item clicked shows a new fragment to the right (similar to gmail and google talk).

To avoid the user from guessing what item has been selected I would like to show the current selection, I tried creating a selector but after it is clicked it changes back to the normal background.

how can I achieve this?

this is my selector xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/list_item_bg2" android:state_pressed="false" android:state_selected="false"     android:state_focused="false"/> <item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="true"/> <item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="false" android:state_focused="false"     android:state_selected="true" android:state_checked="false"/> <item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="false" android:state_focused="true"     android:state_selected="false"/> <item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="false" android:state_focused="false"     android:state_selected="false" android:state_checked="true"/> <item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="true" android:state_focused="true"     android:state_selected="true" android:state_checked="true"/> <item android:drawable="@drawable/list_item_selected_bg2" android:state_pressed="true"/>    </selector>  
like image 574
tyczj Avatar asked Mar 15 '12 23:03

tyczj


People also ask

Which method is used to obtain the selected option from a ListView?

To get which item was selected, there is a method of the ListView called getItemAtPosition.

What is a ListView?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


2 Answers

What Gmail and similar apps use is the activated state, with an appropriate row layout. See:

  • Setting Android Background & Persistence Menu Bar - Using attribute on older versions causes crash - Is there a theme /pattern approach?
  • Change colour of activated list item background on Honeycomb

In a nutshell, you:

  • Use a row layout with an activated background (e.g., android.R.layout.simple_list_item_activated_1)
  • Use setChoiceMode(ListView.CHOICE_MODE_SINGLE) on your ListView
  • "Check" the row that should be activated using setItemChecked() on your ListView to enable the "activated" state and have the persistent highlight
like image 104
CommonsWare Avatar answered Sep 24 '22 00:09

CommonsWare


Your other option is to set the background of your custom list item to android:background="?android:attr/activatedBackgroundIndicator"

like image 22
Jörg Avatar answered Sep 24 '22 00:09

Jörg