Activate CAB menu when OnClickEvent happens in Android

前端 未结 1 1054
野性不改
野性不改 2021-01-13 22:56

Trying to activate CAB menu when clicking on MenuItem from ActionBar. Here is how I set the GridView for listening to Multi Choice. The multiModeChoiceListener is working fi

相关标签:
1条回答
  • 2021-01-13 23:43

    From your question I understand that you're trying to start the GridView associated CAB from clicking one of the menu items. I don't know if you can do this(but I may be mistaken) as the MultiChoiceModeListener expects an item to be checked to start. Depending on your layout and the overall appearance of the GridView, I think you could have a dummy item(as an extra item in the adapter) at the end of the GridView(with no content showing) and use setItemChecked(dummyItemPosition, true) to start the GridView CAB. Of course you'll need to have additional logic to take care of that extra item in your MultiChoiceModeListener:

     public void onItemCheckedStateChanged(ActionMode mode, int position,
            long id, boolean checked) {
        if (position == theDummyPosition)
             return; // so we start the CAB but there aren't any items checked
        }
        int selectCount = gridView.getCheckedItemCount();
        if (selectCount > 0) {
            notify = true;              
            dataArray.add(position);
            // if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it 
            switch (selectCount) {
            case 1:
                mode.setSubtitle("One item added to favorites");
                break;
            default:
                mode.setSubtitle("" + selectCount
                        + " items added to favorites");
                break;
            }
        } 
    
       }
    

    The solution above is a hack, most likely it would be much easier to lose the MultiChoiceModeListener and simply start an ActionMode that you can manipulate for both situations.

    0 讨论(0)
提交回复
热议问题