如何使用 ActionBarDrawerToggle 关闭左右导航抽屉?
How to use ActionBarDrawerToggle to close both left & right navigation drawers?
我的 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>
`
不,我继续在我的工具栏小部件上添加了 ActionBarDrawerToggle,我希望汉堡包图标的行为是,如果我点击它,左抽屉打开(工作),我再次点击它左抽屉关闭(工作),我通过从右向左拖动打开右抽屉加上汉堡包图标变为箭头(工作),如果我点击箭头图标它也会关闭右抽屉(不工作)
如您所见,我希望汉堡包图标根据打开的抽屉来关闭左右抽屉,我的方法是听点击箭头图标并确定哪个抽屉打开然后关闭它。我无法弄清楚如何在 ActionBarDrawerToggle class.
自动添加的汉堡包或箭头图标上设置 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.
我终于想出了解决办法。而不是在 actionBarDrawerToggle 上设置 setToolbarNavigationClickListener,在 Toolbar 上设置 setNavigationOnClickListener 效果很好。我的代码如下。
toolbar.setNavigationOnClickListener(new toolBarNavigationIconListener());
而OnClickListener如下
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);
}
}
}
我的 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>
`
不,我继续在我的工具栏小部件上添加了 ActionBarDrawerToggle,我希望汉堡包图标的行为是,如果我点击它,左抽屉打开(工作),我再次点击它左抽屉关闭(工作),我通过从右向左拖动打开右抽屉加上汉堡包图标变为箭头(工作),如果我点击箭头图标它也会关闭右抽屉(不工作)
如您所见,我希望汉堡包图标根据打开的抽屉来关闭左右抽屉,我的方法是听点击箭头图标并确定哪个抽屉打开然后关闭它。我无法弄清楚如何在 ActionBarDrawerToggle class.
自动添加的汉堡包或箭头图标上设置 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.
我终于想出了解决办法。而不是在 actionBarDrawerToggle 上设置 setToolbarNavigationClickListener,在 Toolbar 上设置 setNavigationOnClickListener 效果很好。我的代码如下。
toolbar.setNavigationOnClickListener(new toolBarNavigationIconListener());
而OnClickListener如下
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);
}
}
}