How to show/Hide Navigation Drawer programmatically

前端 未结 4 848
既然无缘
既然无缘 2021-02-02 05:56

How can I use button to show/hide Navigation Drawer, I have used this SO link to create and manage Navigation Drawer.

Now i am using (Swipe to right from left - to show)

相关标签:
4条回答
  • 2021-02-02 06:39

    to Close Drawer:

    drawer.CloseDrawer((int)GravityFlags.Left);
    

    to Open Drawer:

    drawer.OpenDrawer((int)GravityFlags.Left);
    
    0 讨论(0)
  • 2021-02-02 06:41

    If you are using Sliding Drawer Menu, and you want to hide the menu when it is open (when drag from right to left). Then we have to deal with listview object ontouch listener. The code will be like this.

        //((( When we drage from Right to left then menu hide ))))
        lvMenu.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                switch (event.getAction()) 
                {
                    case MotionEvent.ACTION_DOWN:
                        toggleMenu(v);                  
                        break;
    
                    case MotionEvent.ACTION_UP:
                        //showtoast("up");
                        break;
    
                    default:
                        return false;
                }
                return false;
            }
    
    
        });
    
         public void toggleMenu(View v) {
        mLayout.toggleMenu();
    }
    

    For complete code, you can put the comment here, if you have any problem

    0 讨论(0)
  • 2021-02-02 06:42

    Grab a reference to the DrawerLayout and call closeDrawer(int) to close it and openDrawer(int) to open it. The int parameter refers to the gravity. In your case it should be GravityCompat.LEFT/ GravityCompat.START, because accordingly to the screenshot you posted, your DrawerLayout open and close on the left.

    0 讨论(0)
  • 2021-02-02 06:45

    To open the Drawer

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.openDrawer(GravityCompat.START);
    

    To close the drawer

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    
    0 讨论(0)
提交回复
热议问题