【发布时间】:2012-03-05 07:25:25
【问题描述】:
我最近开始使用 android actionbars 和 contextual action bars (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 但不幸的是,所选列表项不会像此处示例中那样更改背景(选择项目后为浅蓝色背景):
我必须使用自定义选择器吗?或者是否有一个标准程序,android如何处理这个,我只需要让我的LinearLayouts透明?我也尝试了以下但没有成功:
ListView item background via custom selector
如果有人能指出我正确的方向,那就太好了。如果您需要更多应用程序代码或 xml 文件,请告诉我。
【问题讨论】:
标签: android android-layout android-listview android-linearlayout