[英]How to open Drawer Layout only with button?
I'm working on application that has a tab structure, and use sliding movements to move through the tabs. 我正在开发具有标签结构的应用程序,并使用滑动动作在标签中移动。
But now, I want to apply Drawer Layout. 但现在,我想应用抽屉布局。 The problem is that the Drawer has slide to open events.
问题是抽屉有滑动打开事件。 How I can delete this event?
我怎么能删除这个事件? My idea was that the Drawer only could open and close with a button.
我的想法是抽屉只能用按钮打开和关闭。 Is this possible?
这可能吗? Thanks!
谢谢!
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 并使用
openDrawer
和closeDrawer
来更改菜单可见性
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. 默认情况下, DrawerLayout最初是从视图中隐藏的,除非您在触发滑动事件时放置代码以打开
Drawer
。
From the Navigation Drawer example, the contain content_frame
is used to dynamically display views inside the Drawer
using fragments. 从导航抽屉示例中,包含
content_frame
用于使用片段动态显示Drawer
内的视图。
<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, 从
Fragment
的onCreateView()
你可以在一个包含OnClickListener
地方包含一个按钮,在这里放入这段代码,
//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);
}
});
如果要在片段内单击按钮的抽屉项目之间导航,则可以使用此项
((YourMainActivity)getActivity()).selectItem(position);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.