Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Items in Context Action Bar (CAB) dynamically when multiple items are selected

I have a listActivity that shows CAB on long click. If more than 1 item is selected I would like to hide one of my menu items.

I keep track of the # of items selected in onItemCheckedStateChanged(). However I don't have access to the menu to remove the item from this function. See comments in code below to get an idea of what I was trying. I feel like I am missing some simple core understanding... code below is called from my onCreate() function.

private void setupActionBarContext() {
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        private int selCount = 0;
        ArrayList<Long> idList = new ArrayList<Long>();

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (checked) {
                selCount++;
                idList.add(id);
            } else {
                selCount--;
                idList.remove(id);
            }
            mode.setTitle(selCount + " selected");

            // I WOULD LIKE TO HIDE ITEM ON MENU IF 'selCount' IS > 1
            // For example something like this...

            //                if (selCount > 1) {
            //                    MenuItem item = menu.findItem(R.id.edit_item);
            //                    item.setVisible(false);
            //                } else {
            //                    MenuItem item = menu.findItem(R.id.edit_item);
            //                    item.setVisible(false);
            //                }

        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete_item:
                    for(Long i: idList){
                        mDbHelper.deleteItem(i);
                    }
                    mode.finish();
                    return true;
                case R.id.edit_item:
                    Toast.makeText(getBaseContext(), "Edit Item", Toast.LENGTH_SHORT).show();
                    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, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            selCount = 0;
            idList.clear();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
    });

And my menu item...

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/edit_item"
        android:title="@string/edit_item"
        android:showAsAction="ifRoom"
        android:orderInCategory="1"/>
    <item android:id="@+id/delete_item"
        android:title="@string/delete_item"
        android:icon="@drawable/ic_action_delete"
        android:showAsAction="ifRoom"
        android:orderInCategory="2"/>
</menu>
like image 880
Jeremy A. West Avatar asked Jul 14 '13 00:07

Jeremy A. West


1 Answers

As suggested in adneal's comment.

Add invalidate() to onItemCheckedStateChanged()

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
     if (checked) {
         selCount++;
         idList.add(id);
     } else {
         selCount--;
         idList.remove(id);
     }
     mode.setTitle(selCount + " selected");

     mode.invalidate();  // Add this to Invalidate CAB
}

This invalidates the CAB and causes the onPrepareActionMode() function to be called.

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
   if (selCount == 1){
       MenuItem item = menu.findItem(R.id.edit_item);
       item.setVisible(true);
       return true;
   } else {
       MenuItem item = menu.findItem(R.id.edit_item);
       item.setVisible(false);
       return true;
   }
}
like image 189
Jeremy A. West Avatar answered Oct 07 '22 14:10

Jeremy A. West