How to open Drawer Layout only with button?

最后都变了- 提交于 2019-11-28 20:56:05

Just write

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

to prevent menu from listening to gesture

and use openDrawer and closeDrawer to change menu visibility

By default the DrawerLayout is initially hidden from the view unless you put a code to open the Drawer, by the time there is a sliding event triggered.

From the Navigation Drawer example, the contain content_frame is used to dynamically display views inside the Drawer using fragments.

  <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

From the Fragment's onCreateView() you can include a button somewhere that has OnClickListener where in you put this code,

   //For me a better way in avoiding a `null pointer` in getting the DrawerLayout
   final DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
   btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                            //Opens the Drawer
                drawer.openDrawer(Your View, Usually a ListView);
            }

                return false;
        });

You Can also use* to close the drawer.

drawer.closeDrawer(Your View, Usually a ListView);

you can write this way

 mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            drawer.openDrawer(navigationView);

        }
    });
Nandakishore Shetty

If you want to navigate between drawer item on click of button inside your fragments, you can use this

((YourMainActivity)getActivity()).selectItem(position);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!