Android Checkable Menu Item

后端 未结 9 1802
悲哀的现实
悲哀的现实 2020-12-01 00:59

I have the following menu layout in my Android app:




        
9条回答
  •  不思量自难忘°
    2020-12-01 01:52

    READ THIS

    As has been said the "manual checking" is only the tip of the iceberg. It flashes the menu away so fast the users don't see anything happen and it is very counter intuitive, frustrating, and effectively utter crap. The REAL TASK (therefore) is allowing the check box event to be digested by the users mind.

    Good news: this can be done and it does work and this is how you do it. @TouchBoarder had it best so I will copy his code. then develop it.

    the idea is to detect if the checkbox is clicked, then (and only if that one is picked) slightly suppress the menu removal, add a timer for 500ms then close the menu, this give the "tick" animation of the checkbox time to run and creates the right "feel"

    
        
    
    

    then you make this method as usual, but you make sure you add all this extra bumpf

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the bottom bar and the top bar (weird)
        BottomAppBar bottomBar = findViewById(R.id.bottom_app_bar_help);
        Menu bottomMenu = bottomBar.getMenu();
        getMenuInflater().inflate(R.menu.bottom_nav_menu, bottomMenu);
        for (int i = 0; i < bottomMenu.size(); i++) {
            bottomMenu.getItem(i).setOnMenuItemClickListener(item -> {
                if (item.getItemId()==R.id.action_favorite){
                    item.setChecked(!item.isChecked());
                    // Keep the popup menu open
                    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
                    item.setActionView(new View(frmMain.this));
                    item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                        @Override
                        public boolean onMenuItemActionExpand(MenuItem item) {
                            final Handler handler = new Handler();
                            handler.postDelayed(() -> bottomMenu.close(), 500);
                            return false;
                        }
    
                        @Override
                        public boolean onMenuItemActionCollapse(MenuItem item) {
                            final Handler handler = new Handler();
                            handler.postDelayed(() -> bottomMenu.close(), 500);
                            return false;
                        }
                    });
                    return false;
                }
                else {
                    return onOptionsItemSelected(item);
                }
            });
        }
        return true;
    }
    

    the other menu events are here

    public boolean onOptionsItemSelected(MenuItem item) {
        // Bottom Bar item click
        try {
            switch (item.getItemId()) {
                case R.id.mnuExit:
                    MenuClick(ClickType.LOGOUT);
                    return true;
    
                case R.id.mnuList:
                    MenuClick(ClickType.LIST);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onOptionsItemSelected(item);
    }
    

提交回复
热议问题