简体   繁体   中英

Android contextual action bar with custom ListView not allowing multiple selection

I followed Android API guide implementing CAB and I have several problems:

  1. Multiple selection not exist
  2. Selected items (rows) not colored to indicate they are selected
  3. How to change: [A] color of the CAB [B] Show some text on the CAB

The current output is single selection, without coloring the raw and some default color for the action bar. This is my code where MainListAdapter is "regular" implementation of custom ListView adapter, with custom view for each item, and dataList is simple data populate list with:

    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

Presumably, you are not setting up the activated style for your list rows, which governs both of these.

How to change... color of the CAB

A custom theme should be able to do it. The rules should vary somewhat based on whether you are using the native action bar, older versions of appcompat-v7 , or current editions of appcompat-v7 . There are plenty of existing Stack Overflow material on this.

Show some text on the CAB

There is setTitle() and setSubtitle() on ActionMode that you can call.

See this sample app for a demonstration of using the activated style and titles/subtitles. In it, the ListView is in normal mode until the user long-presses on a row, in which case it switches to multiple-choice-modal operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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