How to prevent popup menu from closing on checkbox click

前端 未结 6 1952
無奈伤痛
無奈伤痛 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:10

    In your case R.id.checkbox_item

    return false;
    

    This will tell the system that the event has not yet been handeled and it will not dimiss the menu. See HERE

    0 讨论(0)
  • 2020-12-06 12:18

    Using popupMenu.show() to immediately re-show the popup menu does not work correctly with checkable menu items when changing their checked states.

    Here a method that prevents closing the popup menu in the first place. Make sure that onMenuItemClick returns false.

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
    
            item.setChecked(!item.isChecked());
    
            // Do other stuff
    
            // Keep the popup menu open
            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
            item.setActionView(new View(context));
            item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    return false;
                }
    
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    return false;
                }
            });
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-06 12:26

    The trick here is to show the menu right after it dismisses.
    Below is a sample code snippet:

    popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if(item.getItemId()==R.id.search_by_date_checkbox){
            item.setChecked(!item.isChecked());
        }
        //This is the trick here!!!!
        popupMenu.show();
        return true;
        }
    
    });
    

    You can try this trick with your code! This is how I did it. :)

    0 讨论(0)
  • 2020-12-06 12:28

    Try declaring the PopupMenu globally and calling popup.show(); before returning true in onMenuItemClick function.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-06 12:29

    Got it working by adding popup.show(); on the click of the button and at the end of the click.

    final ImageButton layerButton = (ImageButton) findViewById(R.id.layers);
    final PopupMenu popup = new PopupMenu(MapsActivity.this, layerButton);
    popup.getMenuInflater().inflate(R.menu.toolbar_menu, popup.getMenu());
    
    layerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                //Here
                popup.show();
    
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
    
                        handleClicksOnCheckBoxes(item);
    
                        return true;
    
                    }
    
                });
    
                //And here
                popup.show();
    
    
            }
        });
    

    However, this is not an optimal solution because if the list has so many items that it will be possible to scroll the list, the list will be scrolled to the top when clicking on an item.

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