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

时间:2015-02-12 19:43:06

标签: android android-fragments navigation-drawer

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

Screenshot

2 个答案:

答案 0 :(得分:9)

您需要做一些事情:

  1. 通过设置透明色来禁用布局淡出:

    drawer.setScrimColor(Color.TRANSPARENT);
    
  2. 锁定抽屉

    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
    
  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);
            }
        }
    
    }
    
  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>
    

答案 1 :(得分:2)

这是一个棘手的问题。当抽屉打开时,它会拦截触发事件,触发抽屉关闭。为了防止这种情况,你需要继承DrawerLayout并覆盖onInterceptTouchEvent方法:

public class CustomDrawerLayout extends DrawerLayout
{
    private View rightView;
    private int mTouchSlop;

    public CustomDrawerLayout (Context context)
    {
        this(context, null);
    }

    public CustomDrawerLayout (Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public CustomDrawerLayout (Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context));
    }

    public void setRightView (View v)
    {
        this.rightView = v;
    }

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev)
    {
        boolean result = super.onInterceptTouchEvent(ev);

        if (rightView != null && isDrawerOpen(rightView))
        {
            DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) rightView.getLayoutParams();

            if (layoutParams.gravity == Gravity.END)
            {
                // This is true when the position.x of the event is happening on the left of the drawer (with gravity END)
                if (ev.getX() < rightView.getX() && ev.getX() > mTouchSlop)
                {
                    result = false;
                }
            }
        }

        return result;
    }
}

这是我的代码使用正确的抽屉。我相信你可以适应你的左抽屉。您可能还想禁用阴影:

mDrawerLayout.setScrimColor(Color.TRANSPARENT);