Showing the current selection in a listview

后端 未结 4 832
旧巷少年郎
旧巷少年郎 2020-12-01 04:26

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 wh

相关标签:
4条回答
  • 2020-12-01 04:57

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

    0 讨论(0)
  • 2020-12-01 05:00

    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
    0 讨论(0)
  • 2020-12-01 05:14

    If you use a custom layout for each row:

    1. create a selector with android:state_activated="true"
    2. apply it as the background of the custom layout.

    an example of the selector drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
     android:exitFadeDuration="@android:integer/config_shortAnimTime">
    <item android:state_activated="true" android:drawable="@color/android_green" />
    <item android:drawable="@android:color/transparent" />
    </selector>
    
    0 讨论(0)
  • 2020-12-01 05:14

    What worked for me

    1. override the global ListView Style in your theme

      <item name="android:listViewStyle">@style/MyListView</item>

    2. define your style

      <style name="MyListView" parent="@android:style/Widget.ListView"> <item name="android:listSelector">@drawable/listview_background_selector</item> </style>

    3. define the selector listview_background_selector.xml

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

    4. set the background of your listview-item-layout

      android:background="?android:attr/activatedBackgroundIndicator"

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