第五章实现滑动的7种方式(六七)ViewDragHelper自定义侧滑菜单

ViewDragHelper

你问我这个类是个啥,那我告诉你 知道侧滑菜单DrawLayout不? 他的背后就是它在操控,非常强大!! 
有了这个类,麻麻再也不用担心我自定义半天的控件了,很厉害有木有!!! 
code:

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class MyDrawerLayout extends FrameLayout {

    private ViewDragHelper mViewDragHelper;

    /**
     * 内容视图
     */
    private View mMainView;

    /**
     * 菜单视图
     */
    private View mMenuView;

    private int mMenuWidth;

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

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

    public MyDrawerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // 1.  当xml被inflate结束后调用此方法,获取内容视图和菜单视图
        mMenuView = getChildAt(0);
        mMainView = getChildAt(1);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 2. 在此方法在onMeasure方法后执行,当View的Size发生改变时调用
        // 获取菜单视图的宽度
        mMenuWidth = mMenuView.getMeasuredWidth();
    }

    // 3. 由ViewDragHelper决定是否拦截事件
    @Override
    public boolean onInterceptHoverEvent(MotionEvent event) {
        return mViewDragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 4. 将触摸事件交给ViewDragHelper处理,此操作必不可少
        mViewDragHelper.processTouchEvent(event);
        return true;
    }

    // 创建一个callback
    private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            // 参数parentView的哪个View的哪个子View可以被移动
            // 如果是菜单视图,则可以滑动
            return mMainView == child;
        }

        // ×××××××××只能进行水平滑动
        /**
         * 水平方向的滑动
         * @param child
         * @param left 水平方向child滑动的距离
         * @param dx 增量
         * @return
         */
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (left < 0) { // 不允许向右滑动
                return 0;
            } else if (left > mMenuWidth) { // mainView向右滑动的距离不能超过menuVIew的宽度
                return mMenuWidth;
            }
            return left;
        }

        /**
         * 垂直方向的滑动
         * @param child
         * @param top 垂直方向child移动的距离
         * @param dy 增量
         * @return
         */
        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            // 垂直方向不发生滑动
            return 0;
        }

        /**
         * 拖动结束后调用
         * @param releasedChild
         * @param xvel
         * @param yvel
         */
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            // 这里的侧滑时内容和菜单同时向右滑动,所以用mainView的getLeft()来进行判断
            if (mMainView.getLeft() < mMenuWidth / 2) { //关闭菜单
                // 关闭菜单
                mViewDragHelper.smoothSlideViewTo(mMainView, 0, 0);
                ViewCompat.postInvalidateOnAnimation(MyDrawerLayout.this);
            } else { // close
                mViewDragHelper.smoothSlideViewTo(mMainView, mMenuWidth, 0);
                ViewCompat.postInvalidateOnAnimation(MyDrawerLayout.this);
            }
        }

        // 拖拽状态改变时调用,比如正在拖拽状态,拖拽结束
        @Override
        public void onViewDragStateChanged(int state) {
            super.onViewDragStateChanged(state);
            Log.i("logi", "拖拽状态发生了change" + state);
            /**
             *  #STATE_IDLE  没有被拖动
             *  #STATE_DRAGGING  正在拖动 1
             *  #STATE_SETTLING  拖动结束,位置已经固定
             */
        }

        // changedView位置改变时调用,常用于滑动时更改scale进行缩放等效果
        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            super.onViewPositionChanged(changedView, left, top, dx, dy);
        }

        // 用户触摸到VIew后调用
        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
        }
    };


    private void initView(Context context) {
    // 6. 初始化ViewDragHelper
        mViewDragHelper = ViewDragHelper.create(this, callback);
    }

    // 7. 重写该方法,因为ViewDragHelper也是通过Scroller来进行平滑移动的
    @Override
    public void computeScroll() {
        super.computeScroll();
    // 如果需要继续进行滑动,就刷新界面,类似于incalidate(),但是这个方法是使用工作线程更新界面,更新界面的操作进行在下一个动画帧中 ……
        if (mViewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168

很强大的callback!

测试XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity">

    <com.example.myapplication.MyDrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@drawable/menu"
            android:layout_gravity="left">
        </FrameLayout>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/content">
        </FrameLayout>
    </com.example.myapplication.MyDrawerLayout>

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

效果如下: 
这里写图片描述

 
 
posted @ 2016-11-26 15:56  天涯海角路  阅读(125)  评论(0)    收藏  举报