Android 带有自定义 ListView 的上下文操作栏不允许多选

Android contextual action bar with custom ListView not allowing multiple selection

我遵循了 Android API 实施 CAB 的指南,但我遇到了几个问题:

  1. 多选不存在
  2. 所选项目(行)未着色以表明它们已被选中
  3. 如何更改:[A] CAB 的颜色 [B] 在 CAB 上显示一些文本

当前输出是单选,没有为操作栏的原始颜色和一些默认颜色着色。这是我的代码,其中 MainListAdapter 是自定义 ListView 适配器的 "regular" 实现,每个项目都有自定义视图,而 dataList 是简单的数据填充列表:

    listview = (ListView)findViewById(R.id.listview);
    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 (contextual action bar)
            switch (item.getItemId()) {
                case R.id.menu_delete:
                    deleteSelectedItems();
                    mode.finish();  //Action done, so close the CAB
                    return true;
                case R.id.menu_open:
                    openSelectedItem();
                    mode.finish();  //Action done, so close the CAB
                    return true;
                default:
                    return false;
            }
        }

        private void openSelectedItem() {
            // TODO Auto-generated method stub

        }

        private void deleteSelectedItems() {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            //Inflate the menu for the CAB
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.listmenu, 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.
            //TODO refresh the list
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            //Here you can perform updates to the CAB due to an invalidate() request
            return false;
        }
    });

    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
            //TODO open DisplayActivity
            Toast.makeText(getApplicationContext(), "Open File", Toast.LENGTH_LONG).show();
        }
    });

    listAdapter = new MainListAdapter(dataList, context);
    listview.setAdapter(listAdapter);

Multiple selection not exist

Selected items (rows) not colored to indicate they are selected

据推测,您没有为您的列表行设置 activated 样式,它管理着这两者。

How to change... color of the CAB

自定义主题应该就可以了。根据您使用的是本机操作栏、appcompat-v7 的旧版本还是 appcompat-v7 的当前版本,规则应该有所不同。这上面有plenty of existing Stack Overflow material

Show some text on the CAB

ActionMode上有setTitle()setSubtitle()可以调用。

有关使用激活样式和 titles/subtitles 的演示,请参阅 this sample app。其中,ListView 处于正常模式,直到用户长按一行,在这种情况下,它切换到多选模式操作。