如何以编程方式显示/隐藏导航抽屉

时间:2022-07-29 00:09:01

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

如何使用按钮显示/隐藏导航抽屉,我已使用此SO链接来创建和管理导航抽屉。

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

现在我正在使用(从左侧向右滑动 - 显示)和(从右向左滑动 - 隐藏)

How may I show/Hide Drawer using button highlighted in below screenshot:

如何使用下面屏幕截图中突出显示的按钮显示/隐藏抽屉:

如何以编程方式显示/隐藏导航抽屉

header_home.xml:

<RelativeLayout        
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/header_layout" 
    android:gravity="fill_horizontal" 
    android:layout_gravity="top|center">


 <TextView
    android:id="@+id/textHeader"
    android:text="Home"
    android:textColor="#ffffff"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_header"
 />

 <ImageButton
    android:id="@+id/btnDrawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:adjustViewBounds="true"
    android:background="@drawable/icon_drawer"
    android:contentDescription="@string/app_name"
    />

Edited:

     btnMenu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            drawer.openDrawer(Gravity.LEFT);                
        }
    });

I know to close i have to call drawer.closeDrawer(Gravity.LEFT); but where i have to place this code ?

我知道要关闭我必须调用drawer.closeDrawer(Gravity.LEFT);但我必须放置此代码?

3 个解决方案

#1


48  

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 Gravity.LEFT/ Gravity.START, because accordingly to the screenshot you posted, your DrawerLayout open and close on the left

获取对DrawerLayout的引用并调用closeDrawer(int)来关闭它,并调用openDrawer(int)来打开它。 int参数指的是引力。在你的情况下它应该是Gravity.LEFT / Gravity.START,因为相应于你发布的截图,你的DrawerLayout在左边打开和关闭

#2


1  

to Close Drawer:

关闭抽屉:

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

to Open Drawer:

打开抽屉:

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

#3


-2  

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.

如果您正在使用滑动抽屉菜单,并且想要在打开菜单时(从右向左拖动)隐藏菜单。然后我们必须处理listview对象ontouch监听器。代码将是这样的。

    //((( 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

如果您有任何问题,可以在此处填写评论

#1


48  

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 Gravity.LEFT/ Gravity.START, because accordingly to the screenshot you posted, your DrawerLayout open and close on the left

获取对DrawerLayout的引用并调用closeDrawer(int)来关闭它,并调用openDrawer(int)来打开它。 int参数指的是引力。在你的情况下它应该是Gravity.LEFT / Gravity.START,因为相应于你发布的截图,你的DrawerLayout在左边打开和关闭

#2


1  

to Close Drawer:

关闭抽屉:

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

to Open Drawer:

打开抽屉:

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

#3


-2  

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.

如果您正在使用滑动抽屉菜单,并且想要在打开菜单时(从右向左拖动)隐藏菜单。然后我们必须处理listview对象ontouch监听器。代码将是这样的。

    //((( 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

如果您有任何问题,可以在此处填写评论