我有DrawerLayout设置,左侧有一个抽屉
<FrameLayout android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
<FrameLayout android:id="@+id/navigation_drawer_right"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="end" />
在运行某个方法时会填充右侧抽屉,但是,在运行该方法之前,我可以从右侧滑动,屏幕变暗但没有显示任何内容(因为在方法运行之前布局为空)。
如何在方法运行之前禁用向右滑动?我已经看到完全禁用任何滑动的解决方案,但这对我不起作用,因为左抽屉需要始终工作(当在左抽屉上选择特定选项时调用设置右绘制功能)。
或者,有没有更好的方法来做到这一点?也许在运行函数时动态添加正确的FrameLayout?
答案 0 :(得分:9)
这会禁用滑动:
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_right);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
并启用滑动:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
答案 1 :(得分:4)
有点更新的答案。 您需要通过右侧抽屉视图以禁用其上的滑动。
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, findViewById(R.id.right_drawer));
答案 2 :(得分:4)
提一个有趣的观察。
如果您使用的布局结构具有如下左侧和右侧菜单:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- MAIN CONTAINER -->
<include layout="@layout/content_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
<!-- LEFT SIDE MENU -->
<RelativeLayout
android:id="@+id/left_side_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:visibility="visible"
android:fitsSystemWindows="false"
android:background="@color/green">
<!-- put some menu items in here -->
</RelativeLayout>
<!-- RIGHT SIDE MENU -->
<RelativeLayout
android:id="@+id/right_side_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:visibility="visible"
android:fitsSystemWindows="false"
android:background="@color/gray">
<!-- put some items in here -->
</RelativeLayout>
你会很难尝试只禁用其中一个侧边菜单 像这样的陈述:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, findViewById(R.id.right_drawer))
这将锁定整个DrawerLayout,你将无法滑动任何侧边菜单。
因此,@ Alberto Maluje提出的方法,根据上述答案之一,将为您提供更大的灵活性。
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,GravityCompat.END);