double drawer from the same side ? android drawerlayout

时间:2016-04-07 10:30:59

标签: android android-support-library drawerlayout

in my app i need to display two drawers from the right sides, menu + notifications.

so here is my xml code:

<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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainContent">


    </RelativeLayout>

    <fragment
        android:id="@+id/notification_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:layout_marginLeft="20dp"
        android:background="#111"
        tools:layout="@layout/fragment_drawer_notifications" />

    <fragment
        android:id="@+id/menu_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:layout_marginLeft="20dp"
        tools:layout="@layout/fragment_drawer_menu" />


</android.support.v4.widget.DrawerLayout>

This snipped of code was working, and menu drawer + notification drawer was displayed from the right side. but now i don't know what's happened, i just come back to my old code and it doesn't work. the app crashes and it shows this error:

   java.lang.IllegalStateException: Child drawer has absolute gravity RIGHT but this DrawerLayout already has a drawer view along that edge
    at android.support.v4.widget.DrawerLayout.onMeasure_Original(DrawerLayout.java:1089)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java)
    at android.view.View.measure(View.java:18788)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:18788)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18788)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)

3 个答案:

答案 0 :(得分:0)

问题是你已经将两个片段设置为重力结束,请检查出来:

<?xml version="1.0" encoding="utf-8"?>

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"

    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />
    <ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:choiceMode="singleChoice"
    />

现在主要活动中使用了一些方法和类

assert drawer != null;
    drawer.setDrawerListener(toggle);

toggle.syncState();

        assert drawer != null;
    drawer.closeDrawer(GravityCompat.START);

答案 1 :(得分:0)

由于DNNRegressor不支持同一侧的双抽屉,您可以扩展DrawerLayout并更改某些方法以支持它。

请参阅此file

中国人评论过。但是,班级做了两件事

  1. 处理DrawerLayout抛出的异常,并测量第二个抽屉本身。
  2. 在单击阴影以关闭第一个抽屉时处理DrawerLayout#onMeasure触摸事件。
  3. 注意:该类来自演示,因此您不应复制直接使用它。尝试理解它并修改它以满足您的需求。祝你好运。

答案 2 :(得分:0)

这些天,我遇到了相同的功能。将DrawerLayout包裹在外部DrawerLayout中可以满足要求。布局xml如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:orientation="vertical"
android:overScrollMode="never"
app:layout_anchorGravity="right"
tools:openDrawer="end"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- main content -->
</LinearLayout>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_filters"
    android:layout_gravity="end"
    android:layout_width="250dp"
    android:layout_height="match_parent">

    <RelativeLayout
        android:background="@color/white"
        android:layout_width="250dp"
        android:layout_height="match_parent">

        <!-- right drawer content -->

    </RelativeLayout>

    <!-- inner right drawer content -->
    <ListView
        android:layout_gravity="end"
        android:background="@color/app_green_normal"
        android:id="@+id/lv_filter_options"
        android:layout_width="250dp"
        android:layout_height="match_parent" />

</android.support.v4.widget.DrawerLayout>

需要注意的一件事是,内部抽屉不能被拉出。要打开和关闭时,需要调用openDrawer和closeDrawer。