默认情况下调整内容打开的导航抽屉

问题描述 投票:2回答:1

左边和右边有两个抽屉。现在右抽屉处于锁闭状态,但左抽屉可以打开和关闭。在打开抽屉时,我正在使用抽屉偏移推动中间内容。现在我也想要右抽屉(默认打开)应该移动/调整内容但我无法做到。因为当左抽屉打开时,它会将内容推到右抽屉下方。所以我想要在默认开口处使用正确的抽屉应调整内容,当我打开左侧抽屉内容时应调整,不应在右抽屉下移动。

这是我的代码:

<DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <RelativeLayout
            android:id="@+id/main_screen"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/bg">

            <include
                android:id="@+id/includedView"
                layout="@layout/view" />

        </RelativeLayout>


        <!--  ======= side menu ====== -->
        <RelativeLayout

            android:id="@+id/leftMenu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left">

            <include
                layout="@layout/leftmenuLayout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

        </RelativeLayout>

        <RelativeLayout

            android:id="@+id/rightMenu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right">

            <include
                layout="@layout/rightmenuLayout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

        </RelativeLayout>
        <!-- ======================= -->
    </DrawerLayout>

DrawerAcivity:

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  mDrawerLayout.setScrimColor(Color.parseColor("#00000000"));


        main_screen = (RelativeLayout) findViewById(R.id.main_screen);


        leftMenu= (RelativeLayout) findViewById(R.id.leftMenu);

        rightMenu= (RelativeLayout) 
        findViewById(R.id.rightMenu);

        Display display = getWindowManager().getDefaultDisplay();
        size = new Point();
        display.getSize(size);

        mDrawerLayout.addDrawerListener(new CustomDrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float moveFactor = 0;
                moveFactor = (drawerView.getWidth() * slideOffset);
                main_screen.setTranslationX(moveFactor);

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, rightMenu);
        mDrawerLayout.openDrawer(GravityCompat.END);


        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, leftMenu);


        ViewGroup.LayoutParams params = leftMenu.getLayoutParams();
        params.width = (((size.x / 2) / 4));
        rl_slider_menu.setLayoutParams(params);

        ViewGroup.LayoutParams paramsRight = 
        rightMenu.getLayoutParams();
        paramsRight.width = ((size.x / 3));
        rightMenu.setLayoutParams(paramsRight);

        DrawerLayout.LayoutParams paramsContent = (DrawerLayout.LayoutParams) main_screen.getLayoutParams();
        paramsContent.setMargins(0, 0, (((size.x / 2) / 4)), 0); //substitute parameters for left, top, right, bottom
        main_screen.setLayoutParams(paramsContent);
        main_screen.invalidate();
android android-navigation-drawer
1个回答
2
投票

这实际上是一个非常简单的实现,一旦你的头缠绕它。

我们只需要跟踪两个抽屉的插入测量,将内容View缩放到剩余空间,然后将其翻译为每个插入的一半。例如。建立在问题代码上:

mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
        private float leftInset, rightInset;

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            if (drawerView == leftMenu) {
                leftInset = slideOffset * leftMenu.getWidth();
            }
            else {
                rightInset = slideOffset * rightMenu.getWidth();
            }

            final float scale = 1 - (leftInset + rightInset) / main_screen.getWidth();
            main_screen.setScaleX(scale);

            final float translation = leftInset / 2 - rightInset / 2;
            main_screen.setTranslationX(translation);
        }

        ...
}
© www.soinside.com 2019 - 2024. All rights reserved.