Activate CAB menu when OnClickEvent happens in Android

耗尽温柔 提交于 2019-12-01 06:52:16

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!