打开导航抽屉时,可以使片段可单击

Max*_*tiy 7 android android-fragments navigation-drawer

我的问题如下:我setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN)在平板电脑的横向模式下锁定导航抽屉菜单,但我需要从右边开始激活的片段,所以我可以点击导航始终打开.但我不知道该怎么做.请帮忙.

截图

Sim*_*mas 9

您需要做一些事情:

  1. 通过设置透明颜色禁用布局淡化:

    drawer.setScrimColor(Color.TRANSPARENT);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 锁定抽屉

    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建一个自定义抽屉类,允许在锁定模式下单击:

    public class CustomDrawer extends DrawerLayout {
    
        public CustomDrawer(Context context) {
            super(context);
        }
    
        public CustomDrawer(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomDrawer(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            View drawer = getChildAt(1);
    
            if (getDrawerLockMode(drawer) == LOCK_MODE_LOCKED_OPEN && ev.getRawX() > drawer.getWidth()) {
                return false;
            } else {
                return super.onInterceptTouchEvent(ev);
            }
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在xml中使用此类:

    <com.example.myapplication.CustomDrawer
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- The main content view -->
        </FrameLayout>
        <ListView android:layout_width="100dp"
                  android:layout_height="match_parent"
                  android:layout_gravity="start"
                  android:background="#111"/>
    </com.example.myapplication.CustomDrawer>
    
    Run Code Online (Sandbox Code Playgroud)