Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep highlite list item with SimpleCursorAdapter

i am implementing a splitview using the new fragment from Google (Android 3).

when user choose something from the list it is show value in the details area and keep list item highlight.

when i used array adapter it is keep list item focused after adding the following:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(postition, true);

but when i change to use database using SimpleCursorAdapter and custom row xml file it is highlight just when i press.

"i want to keep highlight item in list view"

like image 775
Alex Avatar asked Oct 15 '11 18:10

Alex


1 Answers

You need to set the activated style for your list row. The catch is that this is only available on API Level 11 and higher.

One way to do this is to use two separate styles. In res/values-v11/styles.xml, you could have:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="activated" parent="android:Theme.Holo">
    <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
  </style>
</resources>

Whereas res/values/styles.xml you would have:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="activated">
  </style>
</resources>

Your row layout would then use that activated style, such as:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:gravity="center_vertical"
  android:layout_marginLeft="4dip"
  android:minHeight="?android:attr/listPreferredItemHeight"
  style="@style/activated"
/>

Combined with your existing CHOICE_MODE_SINGLE logic, this will leave your row activated after it is tapped.

like image 81
CommonsWare Avatar answered Oct 29 '22 12:10

CommonsWare