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

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

更多相关文章

  1. android设置textview限制字数以省略号显示的方法
  2. Android子控件超出父控件方法
  3. Android Studio下载及离线升级方法
  4. Delphi XE5 Android 运行黑屏卡死的解决方法
  5. Android编程: 调试方法
  6. ListView常用属性、方法
  7. Android 控件(button)对齐方法实现详解

随机推荐

  1. OnScrollListenerPro
  2. Android编译时报错:Error:Connection time
  3. Android(安卓)Studio 可视化界面 (Design)
  4. android 上傳圖片的幾種方法
  5. Android(安卓)Studio基本常用快捷键
  6. 揭秘uc浏览器三
  7. Android(安卓)测试工具,实时抓被测app cra
  8. Android(安卓)RectF()用法
  9. 说说Android的广播(5) - 广播的历史
  10. eclipse工程转入android studio后碰见的