繁体   English   中英

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

[英]Make fragment clickable when navigation drawer is opened

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

截图

您需要做一些事情:

  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> 

这是一个棘手的问题。 当抽屉打开时,它会拦截触发事件,触发抽屉关闭。 为了防止这种情况,您需要继承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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM