Android DrawerLayout 侧边栏使用之道

设置左右都能滑动打开侧边栏

android:layout_gravity="start"
android:layout_gravity="end"

2018-5-31 更新以下方法不能很好解决问题

侧边栏中的列表和侧边栏滑动冲突解决

自定义一个侧边栏,控制onInterceptTouchEvent是否拦截事件来解决

public class MQDrawerLayout extends DrawerLayout {

    private int mTouchSlop;
    private float mLastMotionX;
    private float mLastMotionY;
    private boolean isHomePage = true ;

    public MQDrawerLayout(Context context) {
        super(context);
    }

    public MQDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MQDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // 获取最小的滑动距离,超过这个距离才认为是滑动
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 解决侧滑和列表滑动的冲突
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            final float x = ev.getX();
            final float y = ev.getY();
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mLastMotionX = x;
                    mLastMotionY = y;
                    break;
                case MotionEvent.ACTION_MOVE:
                    int xDiff = (int) Math.abs(x - mLastMotionX);
                    int yDiff = (int) Math.abs(y - mLastMotionY);
                    final int x_yDiff = xDiff * xDiff + yDiff * yDiff;
                    boolean xMoved = x_yDiff > mTouchSlop * mTouchSlop;
                    if (xMoved) {
                        if (xDiff > yDiff * 4 && isHomePage) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                    break;
                default:
                    break;
            }
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
        }
        return false;
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            return super.onTouchEvent(ev);
        } catch (IllegalArgumentException ex) {
        }
        return false;
    }

    /**
     * 设置首页拦截滑动事件
     * @param isHomePage
     */
    public void setHomePage(boolean isHomePage) {
        this.isHomePage = isHomePage ;
    }
}

但是这么做还会有一些问题:

  • 如果主页是多个页面会影响主页其他页面布局的左右滑动,特别是其他页面还是ViewPager最为明显,可以通过一个变量来判断是否是主页,是主页则拦截,不是则放开。

  • 只有首页能滑动侧边栏
    也是通过一个变量来判断是否是主页,是主页则拦截,不是则放开。

侧边栏滑出,内容跟随移动

只需要在onDrawerSlide方法中实现即可左右侧滑,内容都跟随移动。

if (mainLeftDrawerLayout.getVisibility() == View.VISIBLE) {
    contentLayout.setX(slideOffset * drawerView.getWidth());
} else if (mainRightDrawerLayout.getVisibility() == View.VISIBLE) {
    contentLayout.setX(slideOffset * (-drawerView.getWidth()));
}

侧边栏填满屏幕

android:layout_marginRight="-65dp"

2018-5-31 更新 以下方法会出现问题,新的解决办法在文末给出,完美解决全屏侧滑问题。

全屏滑动侧边栏

网上有两种方法,笔者这也是 copy 网络上的其中之一的方法,该方法利用反射实现,会有性能问题。而且在参数三设置多大时(0.1 ~ 1.0f),在界面的多大出点击会滑出侧边栏,效果也不是很好。

private void setDrawerEdgeSize(Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage) {
    if (activity == null || drawerLayout == null) return;
    try {
        // 找到 ViewDragHelper 并设置 Accessible 为true
        Field leftDraggerField = drawerLayout.getClass().getDeclaredField("mLeftDragger");// Right
        Field rightDraggerField = drawerLayout.getClass().getDeclaredField("mRightDragger");// Left
        // 让私有变量可以被访问
        leftDraggerField.setAccessible(true);
        rightDraggerField.setAccessible(true);

        ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout);
        ViewDragHelper rightDragger = (ViewDragHelper) rightDraggerField.get(drawerLayout);

        // 找到 edgeSizeField 并设置 Accessible 为true
        Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
        Field edgeRightSizeField = rightDragger.getClass().getDeclaredField("mEdgeSize");

        edgeSizeField.setAccessible(true);
        edgeRightSizeField.setAccessible(true);

        int edgeSize = edgeSizeField.getInt(leftDragger);
        int edgeRightSize = edgeSizeField.getInt(rightDragger);

        // 设置新的边缘大小
        Point displaySize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(displaySize);

        edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (displaySize.x * displayWidthPercentage)));
        edgeSizeField.setInt(rightDragger, Math.max(edgeSize, (int) (displaySize.x * displayWidthPercentage)));

    } catch (NoSuchFieldException e) {
        Log.e(TAG, "setDrawerEdgeSize: e--->" + e.getMessage());
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "setDrawerEdgeSize: e1--->" + e.getMessage());
    } catch (IllegalAccessException e) {
        Log.e(TAG, "setDrawerEdgeSize: e2--->" + e.getMessage());
    }
}

左右滑动侧边栏互斥操作

openclose方法中动态设置即可

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.END);

全屏侧滑的解决之道

参考了该仁兄的办法,确实有效,在此感谢了!
参考链接 Android自定义View,实现全屏滑动的DrawerLayout

由于没有积分,下不了Demo,所以就自己参考该仁兄文中说的核心代码加上业务的需要进行了如下实现,代码仅供参考!如有帮助,请点个 star

测试代码,写的乱些,多担待。
链接地址:GitHub

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_龙衣

赏杯快乐水喝喝

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值