How to prevent popup menu from closing on checkbox click

前端 未结 6 1951
無奈伤痛
無奈伤痛 2020-12-06 11:30

I search a lot on net but there is nothing about preventing popup menu from closing.

Whenever i click on checkbox item or any other popup menu item, popup menu dismi

6条回答
  •  温柔的废话
    2020-12-06 12:29

    Oliver's answer above (https://stackoverflow.com/a/31727213/2423194) gave me a crash, and its message told me to use MenuItemCompat instead. After some tweaking to this code, it works:

    // Keep the popup menu open              
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    item.setActionView(new View(getContext()));
    MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return false;
        }
    
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return false;
        }
    });
    

    Thanks Oliver!

提交回复
热议问题