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

egdjgwm8  于 2023-01-15  发布在  Android
关注(0)|答案(8)|浏览(177)

我有一个应用程序,我想在其中实现一个双抽屉-一个从左边,一个从右边。左边的抽屉是应用程序导航,右边的抽屉是结果过滤。
因此,布局是这样的:

<?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”,以及它们各自的重力-“start”和“end”这真的有效!你可以把它们都拉出来。
问题是,当我实现DrawerToggle时-它只打开左边的抽屉,而不关闭右边的抽屉,所以如果右边的抽屉打开,我按下DrawerToggle按钮-左边的抽屉也打开,并与右边的抽屉重叠。
有一对夫妇的解决方案,我试图得到:
1.在右侧制作相同的DrawerToggle按钮,其行为和动画与左侧相同。
1.使抽屉位于我试图打开的抽屉的另一侧-自动关闭(如果左侧抽屉打开,我按下右侧抽屉的开关,反之亦然)。
我还没有想出如何做到这一点,因为DrawerToggle接受DrawerLayout本身作为参数,而不是单个抽屉...
我正在使用支持库。
有人有什么想法吗?先谢谢你。

7vhp5slm

7vhp5slm1#

以下是Double Drawer Activity的代码,可以由其他Activity扩展以实现该Double Drawer Activity,假设它们具有类似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);
    }
}
blmhpbnm

blmhpbnm2#

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

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

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

c2e8gylq

c2e8gylq3#

可以从材料设计中使用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>
4jb9z9bj

4jb9z9bj4#

这里是我的简短的解决方案,如果你想阻止抽屉指示器的动画,如果他们滑动右视图。简单地实现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
       }
    }
};
i1icjdpr

i1icjdpr5#

在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 =实现绘图布局的侦听器对象。绘图侦听器
参见:http://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

rbl8hiat

rbl8hiat6#

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

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

pw9qyyiw

pw9qyyiw7#

做一个自定义项目,并添加到它的权利,传递给它的权利抽屉。

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

hc8w905p8#

您可以在同一抽屉中显示两个导航视图,如:

<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
   <com.google.android.material.navigation.NavigationView
    android:id="@+id/NAVIGATION_VIEW_LEFT"
    android:layout_width="270dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"/>
   <com.google.android.material.navigation.NavigationView
        android:id="@+id/NAVIGATION_VIEW_RIGHT"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"/>
</androidx.drawerlayout.widget.DrawerLayout>

对于活动,请执行以下操作:

actionBarDrawerToggle = new ActionBarDrawerToggle(context, drawerLayout, R.string.nav_open, R.string.nav_close);
        // pass the Open and Close toggle for the drawer layout listener
        // to toggle the button
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        nav_drawer_left_Iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
        nav_drawer_right_Iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.END);
            }
        });

相关问题