简体   繁体   English

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

[英]How to show/Hide Navigation Drawer programmatically

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: 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);我知道要关闭我必须调用drawer.closeDrawer(Gravity.LEFT); but where i have to place this code ?但是我必须把这段代码放在哪里?

Grab a reference to the DrawerLayout and call closeDrawer(int) to close it and openDrawer(int) to open it.获取对DrawerLayout的引用并调用closeDrawer(int)关闭它并调用 openDrawer(int)打开它。 The int parameter refers to the gravity. int参数指的是重力。 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.在您的情况下,它应该是GravityCompat.LEFT / GravityCompat.START ,因为根据您发布的屏幕截图,您的DrawerLayout在左侧打开和关闭。

to Close Drawer:关闭抽屉:

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

to Open Drawer:打开抽屉:

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

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);

I might be late for this but here is the solution you are looking for:我可能会迟到,但这是您正在寻找的解决方案:

btnMenu.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         if(drawer.isDrawerOpen(GravityCompat.START)){
                drawerLayout.closeDrawer(GravityCompat.START);
            }
            else {
                drawerLayout.openDrawer(GravityCompat.START);
            }                
    }
});

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.然后我们必须处理listview 对象ontouch 侦听器。 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对于完整的代码,你可以把评论放在这里,如果你有任何问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM