简体   繁体   中英

How to use ActionBarDrawerToggle to close both left & right navigation drawers?

I have the following layout in my activity.xml

`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/store_page_layout"
    tools:context=".StorePage">
    <include
        android:id="@+id/store_page_toolbar"
        layout="@layout/toolbar"/>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/store_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/store_page_toolbar">
        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/container_body_height"
            android:layout_weight="1">
            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view"
                android:layout_gravity="center"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginRight="16dp"
                android:layout_marginLeft="16dp"
                android:elevation="6dp"
                card_view:cardCornerRadius="4dp"
                android:background="@drawable/landing_animated_button_background">
            </android.support.v7.widget.CardView>
        </FrameLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/store_menu_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/black_200"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/store_cart_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_marginLeft="@dimen/nav_drawer_left_min_margin"
            android:background="@color/black_200"/>
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

`

No I went ahead and added ActionBarDrawerToggle on my Toolbar widget, the behavior I wanted from the hamburger icon is if I click on it left drawer open up (Working), I click it again left drawer closes (Working), I open right drawer by dragging from right to left plus the hamburger icon changes to arrow (Working), if I click on arrow icon it closes right drawer as well (Not working)

As you can see, I want the hamburger icon to close both right and left drawers based on which one is open, my approach is to listen to click on arrow icon and decide which drawer is open then close it. I am unable to figure out how to set onClickListener on the hamburger or arrow icon automatically added by ActionBarDrawerToggle class.

This is explained in the documentation

http://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html#setToolbarNavigationClickListener(android.view.View.OnClickListener)

public void setToolbarNavigationClickListener (View.OnClickListener onToolbarNavigationClickListener)

When DrawerToggle is constructed with a Toolbar, it sets the click listener on the Navigation icon. If you want to listen for clicks on the Navigation icon when DrawerToggle is disabled (setDrawerIndicatorEnabled(boolean), you should call this method with your listener and DrawerToggle will forward click events to that listener when drawer indicator is disabled.

I finally figured out the solution. Instead of setting setToolbarNavigationClickListener on actionBarDrawerToggle setting setNavigationOnClickListener on Toolbar works perfectly. my code is given below.

toolbar.setNavigationOnClickListener(new toolBarNavigationIconListener());

And OnClickListener is as follows.

    private class toolBarNavigationIconListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        if(!storeDrawer.isDrawerOpen(Gravity.RIGHT) && !storeDrawer.isDrawerOpen(Gravity.LEFT)) {
            storeDrawer.openDrawer(Gravity.LEFT);
        } else if(storeDrawer.isDrawerOpen(Gravity.LEFT)) {
            storeDrawer.closeDrawer(Gravity.LEFT);
        } else if(storeDrawer.isDrawerOpen(Gravity.RIGHT)) {
            storeDrawer.closeDrawer(Gravity.RIGHT);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM