简体   繁体   English

如何在没有长单击事件的情况下在 Android 中触发上下文操作栏 (CAB) 以便从 Android 中的 ListView 中选择多个项目

[英]How to trigger contextual action bar (CAB) in Android without long-click event in order to select multiple items from ListView in Android

I've just learned how to create listview but do not know how to select multiple items without long-click on any item.我刚刚学会了如何创建列表视图,但不知道如何在长按任何项目的情况下选择多个项目。 I want trigger CAB after pressing button "Select" from action bar and then delete selected items.我想在按下操作栏中的“选择”按钮后触发 CAB,然后删除所选项目。 Here are the code with long-click event from developer.android.com:以下是来自 developer.android.com 的带有长按事件的代码:

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

http://www.technotalkative.com/contextual-action-bar-cab-android/ http://www.technotalkative.com/contextual-action-bar-cab-android/

tldr; tldr;

1) Implement the ActionMode.Callback interface. 1) 实现 ActionMode.Callback 接口。

2) Call startActionMode() when you want to show the bar. 2) 当你想显示栏时调用 startActionMode()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM