自定义列表视图上的选定项目与上下文操作栏

我最近开始使用android 操作栏和上下文操作栏 (CAB)。

我只有一个活动,这是一个ListActivity。 基本上我使用下面的代码来“激活”CAB:

ListView listView = getListView(); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { // Here you can do something when items are selected/de-selected, // such as update the title in the CAB } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // Respond to clicks on the actions in the CAB switch (item.getItemId()) { case R.id.menu_delete: deleteSelectedItems(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate the menu for the CAB MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { // Here you can make any necessary updates to the activity when // the CAB is removed. By default, selected items are deselected/unchecked. } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // Here you can perform updates to the CAB due to // an invalidate() request return false; } }); 

列表的布局:

 <ImageView android:id="@+id/message_on_clipboard_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:minWidth="30dp" android:padding="7sp" android:src="@android:drawable/presence_online" > </ImageView> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/listitem_background" android:orientation="vertical" > <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="5sp" android:fadingEdge="horizontal" android:singleLine="true" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" > </TextView> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/listitem_background" android:orientation="horizontal" > <TextView android:id="@+id/message_length" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5sp" android:text="@string/message_length" android:textAppearance="?android:attr/textAppearanceSmall" > </TextView> <TextView android:id="@+id/message_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5sp" android:text="TextView" android:textAppearance="?android:attr/textAppearanceSmall" > </TextView> <TextView android:id="@+id/date_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5sp" android:text="@string/date_label" > </TextView> <TextView android:id="@+id/date_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" > </TextView> </LinearLayout> </LinearLayout> 

在main.xml中:

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

现在,如果我长时间点击一个列表项目,CAB就像预期的那样显示出来:

出租车

我使用了MultiChoiceModeListener,但不幸的是,所select的列表项目不会像这里的例子那样改变背景(select一个项目之后浅蓝色背景):

在这里输入图像说明

我必须使用自定义select器吗? 或者是有一个标准的程序如何处理这个和我只是需要使我的LinearLayouts透明? 我也尝试了以下,但没有成功:

ListView项目背景通过自定义select器

如果有人能把我指向正确的方向,那将是很棒的。 请让我知道,如果你需要更多的应用程序代码或XML文件。

我只在CHOICE_MODE_SINGLE中testing过这个,但是在这种情况下,它的工作原理如下。

  • 当您在代码中select列表项时,请在列表中为该项调用“setItemChecked(position,checked)”方法(在ListView实例上)。

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

在路上创build一个名为custom_background的drawable:

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

只尝试支持ICS设备,但是这种新的布局可以达到你想要达到的目的,保持选中的行高亮显示。

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

对于任何人仍然遇到这个问题,请检查您的android:minSdkVersion。 如果太低,select器背景颜色可能不起作用。