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

时间:2015-05-29 10:41:40

标签: android android-listview menuitem contextual-action-bar android-contextmenu

我遵循了实施CAB的Android API指南,我遇到了一些问题:

  1. 不存在多项选择
  2. 未选中的所选项目(行)表示已选中
  3. 如何更改:[A] CAB的颜色[B]在CAB上显示一些文字
  4. 当前输出是单选,不会为操作栏的原始颜色和某些默认颜色着色。这是我的代码,其中MainListAdapter是自定义ListView适配器的“常规”实现,每个项目都有自定义视图,而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);
    

1 个答案:

答案 0 :(得分:2)

  

不存在多项选择

     

未选中的所选项目(行)表示已选中

据推测,您没有为列表行设置activated样式,这些行管理这两种行。

  

如何更改CAB的颜色

自定义主题应该能够做到。根据您使用的是原生操作栏,appcompat-v7的旧版本还是appcompat-v7的当前版本,规则应有所不同。此问题有plenty existing Stack Overflow material

  

在CAB上显示一些文字

您可以致电setTitle() setSubtitle()ActionMode

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