Gần đây tôi đã bắt đầu sử dụng android actionbars và contextual action bars (CAB).mục đã chọn trên chế độ xem danh sách tùy chỉnh với thanh tác vụ theo ngữ cảnh
Tôi chỉ có một hoạt động là ListActivity. Về cơ bản tôi sử dụng đoạn mã sau snipped để "kích hoạt" 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;
}
});
Cách bố trí của danh sách:
<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>
Và trong main.xml:
<ListView
android:id="@+android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
Bây giờ nếu tôi làm một nhấp chuột dài trên một mục danh sách CAB xuất hiện như mong đợi:
tôi sử dụng một MultiChoiceModeListener nhưng tiếc là danh sách các mục đã chọn không thay đổi nền như trong ví dụ ở đây (ánh sáng nền màu xanh sau khi một mục được chọn):
tôi có phải sử dụng một bộ chọn tùy chỉnh? Hoặc là có một thủ tục tiêu chuẩn như thế nào Android không xử lý này và tôi chỉ cần làm cho LinearLayouts của tôi minh bạch? Tôi cũng đã cố gắng như sau nhưng không thành công:
ListView item background via custom selector
Nó sẽ là tuyệt vời nếu ai đó có thể chỉ cho tôi đi đúng hướng. Vui lòng cho tôi biết nếu bạn cần thêm mã ứng dụng hoặc tệp xml.
Tôi nhận được một StackOverflowError nếu tôi làm như sau trong 'onItemCheckedStateChanged()': 'listView.setItemChecked (vị trí, kiểm tra);' – domi
nvm. nó hoạt động chỉ với: 'android: background ="? android: attr/activatedBackgroundIndicator "' cảm ơn bạn rất nhiều! – domi
Là một bổ sung cho nhận xét của domi: nếu bạn muốn tùy chỉnh giao diện của các hàng đã chọn, bạn có thể thực hiện bằng cách sử dụng bố cục của hàng của riêng bạn, như được mô tả trong hướng dẫn này: http: // www. marvinlabs.com/2010/10/custom-listview-ability-check-items/ – Konsumierer