我想在longClick()上启用多视图选择。我应该声明一个动作模式对象并调用startActionMode()吗?另外,如何更改单项单击的菜单列表?如图所示,我将文档用作参考。

    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;
}


});

最佳答案

要更改单个项目的菜单列表,请单击下面的代码。

int count=0;
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
                                  long id, boolean checked) {
if (checked) {
            count++;
        } else {
            count--;
        }
mode.invalidate();  // Add this to Invalidate CAB so that it calls onPrepareActionMode
}


现在,如下修改onPrepareActionMode

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selCount == 1){
   //show the menu here that you want if only 1 item is selected
} else {
  //show the menu here that you want if more than 1 item is selected
       }
}

07-24 20:39