Android 带有上下文操作栏的自定义listview上的选定项

Android 带有上下文操作栏的自定义listview上的选定项,android,android-layout,android-listview,android-linearlayout,Android,Android Layout,Android Listview,Android Linearlayout,我最近开始使用android和(CAB) 我只有一个活动,它是ListActivity。基本上,我使用以下截取的代码“激活”驾驶室: 列表的布局: 在main.xml中: <ListView android:id="@+android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> 现在,如

我最近开始使用android和(CAB)

我只有一个活动,它是ListActivity。基本上,我使用以下截取的代码“激活”驾驶室:

列表的布局:


在main.xml中:

<ListView
    android:id="@+android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>
</ListView>

现在,如果我长时间单击列表项,CAB将按预期显示:

我使用的是MultiChoiceModelListener,但不幸的是,所选列表项不会像这里的示例中那样更改背景(选择项后的浅蓝色背景):

我必须使用自定义选择器吗?或者有没有一个标准的程序,android如何处理这个问题,我只需要让我的线性布局透明?我还尝试了以下方法,但没有成功:


如果有人能给我指出正确的方向那就太好了。如果您需要更多的应用程序代码或xml文件,请告诉我。

我只在CHOICE\u MODE\u SINGLE中测试过,但在这种情况下,它可以通过执行以下操作来工作

  • 选择列表项时,在代码中,为列表中的该项调用“setItemChecked(position,checked)”方法(在ListView实例上)

  • 将其添加到单个ListView项目的XML中:
    android:background=“?android:attr/activatedBackgroundIndicator”


仅尝试支持ICS设备及以上,但此新布局实现了您想要实现的目标,即突出显示所选线路

adapter = new SimpleCursorAdapter(getActivity(),
     android.R.layout.simple_list_item_activated_1, 
     null, 
     from, 
     to,
     0);

只需在途中创建一个名为custom_background的可绘制文件:

<?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>

对于仍然遇到此问题的任何人,请检查您的android:Minsdk版本。如果太低,选择器背景色可能不起作用。

如果我在
onItemCheckedStateChanged()
中执行以下操作,则会出现StackOverflower错误:
listView.setItemChecked(位置,选中)nvm。它只适用于:
android:background=“?android:attr/activatedBackgroundIndicator”
非常感谢!调用setItemChecked将导致StackOverflowerError,因为在onItemCheckedStateChanged中调用setItemChecked将只是无限地相互调用。对于列表
listChoiceBackgroundIndicator
是更好的选择。对于@domi,通过设置此
listView.setItemChecked(位置,选中)
,您一次又一次地调用
onItemCheckedStateChanged
方法,这导致对该方法的调用减少,最终导致StackOverflowerError。希望它能解决您的疑问。完美!我创建了选择器并将其作为网格视图项背景应用。谢谢
adapter = new SimpleCursorAdapter(getActivity(),
     android.R.layout.simple_list_item_activated_1, 
     null, 
     from, 
     to,
     0);
<?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>
android:background="@drawable/custom_background"