41

Gần đây tôi đã bắt đầu sử dụng android actionbarscontextual 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:

cab

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

enter image description here

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.

Trả lời

52

Tôi chỉ từng thử nghiệm điều này trong CHOICE_MODE_SINGLE, nhưng trong trường hợp đó, nó hoạt động bằng cách thực hiện như sau.

  • Khi bạn chọn mục danh sách, trong mã, hãy gọi phương thức "setItemChecked (position, checked)" (trên thể hiện ListView) cho mục đó trong danh sách.

  • Thêm phần này vào XML cho các hạng mục ListView cá nhân:
    android:background="?android:attr/activatedBackgroundIndicator"

+6

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

+8

nvm. nó hoạt động chỉ với: 'android: background ="? android: attr/activatedBackgroundIndicator "' cảm ơn bạn rất nhiều! – domi

+0

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

0

Chỉ cố gắng để hỗ trợ các thiết bị ICS và lên nhưng cách bố trí mới này không những gì bạn đang cố gắng để đạt được, trong việc giữ dòng chọn nhấn mạnh.

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

Chỉ cần tạo aa drawable gọi custom_background trên đường:

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

và thiết lập như là nền tảng về bố trí cha mẹ của bạn:

android:background="@drawable/custom_background" 
+0

Hoàn hảo! Tôi đã tạo bộ chọn và áp dụng nó làm nền của khung nhìn dạng lưới. Cảm ơn. –

0

Đối với bất cứ ai vẫn gặp vấn đề này, xin vui lòng kiểm tra của bạn android: minSdkVersion. Nếu nó quá thấp, màu nền của bộ chọn có thể không hoạt động.