5

我有 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?

4

3 回答 3

9

这将禁用滑动:

mDrawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_right);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

并启用滑动:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
于 2013-10-25T14:58:22.410 回答
4

有点更新的答案。您需要通过右抽屉视图来禁用滑动它。

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, findViewById(R.id.right_drawer));
于 2014-02-12T05:30:43.013 回答
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);

于 2016-04-06T10:00:43.663 回答