DrawerLayout Double Drawer (Left and Right Drawers

2019-01-03 12:50发布

I have an application, in which i want to implement a double drawer - one from the left and one from the right. Left drawer is for app navigation, right drawer is for result filtering.

So, the layout is like this:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/light_grey"
        android:orientation="vertical">

        <GridView
            android:id="@+id/gridview"
            style="@style/GridViewStyle"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:horizontalSpacing="7dp"
            android:stretchMode="columnWidth"
            android:verticalSpacing="7dp" />
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

You can clearly see here "left_drawer" and "right_drawer", and their respective gravity - "start" and "end" And this actually works! You can pull them both out.

The problem is, when i implement the DrawerToggle - it only opens the left drawer, and does not close the right one, so if the right drawer is opened and i press the DrawerToggle button - the left drawers opens ALSO, and overlaps the right drawer.

There are a couple of solutions i'am trying to get:

  1. Make the same DrawerToggle button on the right side, with the same behavior and animation as the left side.
  2. Make the drawer on the opposite side of the drawer i am trying to open - automatically close (if the left drawer is open and i press the toggle of the right drawer and vise-versa).

And i haven't figured how to do that, because DrawerToggle accepts the DrawerLayout itself as a parameter, and not the individual drawers...

I am using the Support Library.

Anyone have any ideas? Thank you in advance.

7条回答
聊天终结者
2楼-- · 2019-01-03 13:19

I have solved adding this code in the onOptionsItemSelected method:

switch (item.getItemId()) {
    case android.R.id.home:
        if (mDrawerLayout.isDrawerOpen(mDrawerList_right)){
            mDrawerLayout.closeDrawer(mDrawerList_right);
        }
        mDrawerLayout.openDrawer(mDrawerList_left);
    }
    break;
case R.id.action_drawer:
        if (mDrawerLayout.isDrawerOpen(mDrawerList_left)){
            mDrawerLayout.closeDrawer(mDrawerList_left);
        }
        mDrawerLayout.openDrawer(mDrawerList_right);
    }
default:
    break;
}

I have added an action button and overrided the home button of the actionbar

查看更多
登录 后发表回答