DrawerLayout双抽屉(同时左右抽屉)
我有一个应用程序,在这个应用程序中,我想实现一个双抽屉 – 左边一个,右边一个。 左侧抽屉用于应用程序导航,右侧抽屉用于结果过滤。
所以,布局是这样的:
<?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>
你可以清楚的看到这里的“left_drawer”和“right_drawer”,以及它们各自的引力 – “开始”和“结束”而这个实际上是有效的! 你可以把它们拉出来。
问题是,当我执行DrawerToggle时,它只打开左边的抽屉,并没有closures右边的抽屉,所以如果右边的抽屉被打开,我按下DrawerTogglebutton,左边的抽屉也会打开,并且重叠右边的抽屉。
有几个解决scheme我试图得到:
- 在右侧制作相同的DrawerTogglebutton,具有与左侧相同的行为和animation。
- 将抽屉放在我想打开的抽屉对面 – 自动closures(如果左抽屉打开,我按下右抽屉的切换键,反之亦然)。
而我还没有想到如何做到这一点,因为DrawerToggle接受DrawerLayout本身作为参数,而不是个别的抽屉…
我正在使用支持库。
任何人有任何想法? 先谢谢你。
你可以在ToggleButton的处理程序中调用它,例如:
mDrawerLayout.openDrawer(mDrawer); mDrawerLayout.closeDrawer(mDrawer);
其中mDrawer是对特定抽屉的引用,您需要打开(无论是视图还是布局),在您的情况下,您希望显示的实际ListView。
这里是一个双抽屉活动的代码,比其他活动可以扩展来实现双抽屉,假设他们有一个像OP所提供的布局。
public class DoubleDrawerActivity extends ActionBarActivity { private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; private View mLeftDrawerView; private View mRightDrawerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); } @Override protected void onStart() { super.onStart(); if(mDrawerLayout == null || mLeftDrawerView == null || mRightDrawerView == null || mDrawerToggle == null) { // Configure navigation drawer mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mLeftDrawerView = findViewById(R.id.left_drawer); mRightDrawerView = findViewById(R.id.right_drawer); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer, R.string.drawer_open, R.string.drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View drawerView) { if(drawerView.equals(mLeftDrawerView)) { getSupportActionBar().setTitle(getTitle()); supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() mDrawerToggle.syncState(); } } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { if(drawerView.equals(mLeftDrawerView)) { getSupportActionBar().setTitle(getString(R.string.app_name)); supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() mDrawerToggle.syncState(); } } @Override public void onDrawerSlide(View drawerView, float slideOffset) { // Avoid normal indicator glyph behaviour. This is to avoid glyph movement when opening the right drawer //super.onDrawerSlide(drawerView, slideOffset); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); // Set the drawer toggle as the DrawerListener } } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view for(int i = 0; i< menu.size(); i++) menu.getItem(i).setVisible(!mDrawerLayout.isDrawerOpen(mLeftDrawerView)); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case android.R.id.home: mDrawerToggle.onOptionsItemSelected(item); if(mDrawerLayout.isDrawerOpen(mRightDrawerView)) mDrawerLayout.closeDrawer(mRightDrawerView); return true; } return super.onOptionsItemSelected(item); } }
对于所有想要防止抽屉指示器animation的人来说,这是我的简短解决scheme。 只需像这样实现onDrawerSlide方法。
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer_white, 0, 0) { @Override public void onDrawerClosed(View view) { invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } @Override public void onDrawerOpened(View drawerView) { invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } @Override public void onDrawerSlide(View drawerView, float slideOffset) { if (drawerView == mSlidingMenuNavigationList) { super.onDrawerSlide(drawerView, slideOffset); } else { // do nothing on all other views } } };
您可以从材质devise中使用NavigationView
。
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <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" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="end" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer1" /> </android.support.v4.widget.DrawerLayout>
欲了解更多信息,请参阅http://v4all123.blogspot.in/2016/03/simple-example-of-navigation-view-on.html
使用onOptionsItemSelected()中要closures的任何抽屉的重力常量(Gravity.LEFT或Gravity.RIGHT)(如同打开另一个抽屉),如下所示。
public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { // Close the right side drawer if visible if(mDrawerLayout.isDrawerVisible(Gravity.RIGHT)) { mDrawerLayout.closeDrawer(Gravity.RIGHT); } return true; } // Regular stuff switch (item.getItemId()) { case R.id.action_example: Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); }
mDrawerToggle =实现DrawerLayout.DrawerListener的监听器对象
请参阅: http : //developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html
我已经解决了在onOptionsItemSelected方法中添加这个代码:
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; }
我添加了一个操作button,并覆盖了操作栏的主页button
制作一个自定义项目并将其添加到右侧,并将其传递给右侧的抽屉。
final ToggleButton ic_nav = (ToggleButton) customNav.findViewById(R.id.ic_nav); ic_nav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if ( mDrawerLayout.isDrawerOpen(mDrawerList) && arg0.isSelected()) { mDrawerLayout.closeDrawer(mDrawerList); arg0.setSelected(false); } else if (!mDrawerLayout.isDrawerOpen(mDrawerList) && !arg0.isSelected()){ mDrawerLayout.openDrawer(mDrawerList); ic_nav.setSelected(false); arg0.setSelected(true); } } });