DrawerLayout双抽屉(左右抽屉同时)

时间:2013-07-25 15:05:24

标签: android navigation-drawer 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时 - 它只打开左抽屉,而不是关闭右抽屉,所以如果打开右抽屉并按下DrawerToggle按钮 - 左抽屉也会打开,并重叠合适的抽屉。

我想要获得一些解决方案:

  1. 在右侧制作相同的DrawerToggle按钮,其行为和动画与左侧相同。
  2. 将抽屉放在我试图打开的抽屉的另一侧 - 自动关闭(如果左抽屉打开,我按下右抽屉的拨动开关,反之亦然)。
  3. 我还没想过怎么做,因为DrawerToggle接受DrawerLayout本身作为参数,而不是单个抽屉......

    我正在使用支持库。

    有人有什么想法吗? 提前谢谢。

6 个答案:

答案 0 :(得分:36)

这是双抽屉活动的代码,可以通过其他活动扩展来实现双抽屉,假设它们的布局类似于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);
    }
}

答案 1 :(得分:22)

你可以在ToggleButton的处理程序中调用它,例如:

mDrawerLayout.openDrawer(mDrawer);
mDrawerLayout.closeDrawer(mDrawer);

其中mDrawer是您需要打开的特定抽屉的引用(无论是视图还是布局),在您的情况下,是您希望显示的实际ListView。

答案 2 :(得分:6)

这是我的所有想要阻止抽屉指示器动画的人的简短解决方案,如果他们滑动右视图。只需像这样实现 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
       }
    }
};

答案 3 :(得分:5)

使用onOptionsItemSelected()中要关闭的任何抽屉(当打开另一个抽屉时)的重力常数(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

答案 4 :(得分:0)

我已经解决了在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;
}

我添加了一个操作按钮并覆盖了操作栏的主页按钮

答案 5 :(得分:0)

制作一个自定义项目并将其添加到右侧,将右侧抽屉传递给它。

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);
                }
            }
        });