selected item on custom listview with contextual action bar

后端 未结 4 1960
一整个雨季
一整个雨季 2021-01-30 10:51

I recently started using android actionbars and contextual action bars (CAB).

I have just one activity which is a ListActivity. Basically I use the following code snippe

相关标签:
4条回答
  • 2021-01-30 11:14

    Only trying to support ICS devices and up but this new layout does what you are trying to achieve, in keeping the selected line highlighted.

    adapter = new SimpleCursorAdapter(getActivity(),
         android.R.layout.simple_list_item_activated_1, 
         null, 
         from, 
         to,
         0);
    
    0 讨论(0)
  • 2021-01-30 11:18

    Just create a a drawable called custom_background on the way:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/highlight" android:state_activated="true"/>
        <item android:drawable="@color/normal"/>
    </selector>
    

    and set as background on your parent layout:

    android:background="@drawable/custom_background"
    
    0 讨论(0)
  • 2021-01-30 11:20

    For anyone still meet this issue, please check your android:minSdkVersion. If it's too low, selector background color may not work.

    0 讨论(0)
  • 2021-01-30 11:26

    I've only ever tested this in CHOICE_MODE_SINGLE, but in that situation it works by doing the following.

    • When you select a list item, in code, call "setItemChecked(position, checked)" method (on the ListView instance) for that item in the list.

    • Add this to the XML for individual ListView items:
      android:background="?android:attr/activatedBackgroundIndicator"

    0 讨论(0)
提交回复
热议问题