selected item on custom listview with contextual action bar

你说的曾经没有我的故事 提交于 2019-12-02 16:53:21
Alexander Lucas

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"

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"

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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!