代码库/DragView/自由拖动

ViewDragHelper

                                                                                DrawerLayout/SlidingPaneLayout

1.初始化ViewDragHelper

    ViewDragHelper通常定义在一个ViewGroup内部,并通过其静态工厂方法进行初始化

          实例化:ViewDragHelper.creat(this,callback)

                        this:要监听的View通常是一个ViewGroup(parentView)

2.拦截事件

    重写拦截方法 onInterceptTouchEvent(MotionEvent ev) / return mViewDragHelper.shouldInterceptTouchEvent(ev)

    在onTouchEvent方法中,将触摸事件也传递给ViewDragHelper  

mViewDragHelper.processTouchEvent(event);
return true;

3.处理computeScroll()

    模板:

@Override
public void computeScroll() {
    if (mViewDragHelper.continueSettling(true)){
        ViewCompat.postInvalidateOnAnimation(this);
    }
    super.computeScroll();}

4.处理回调Callback

private ViewDragHelper.Callback  callback=new ViewDragHelper.Callback()

tryCaptureView指定创建ViewDragHelper时ViewGroup中哪个子View可以被移动

具体的滑动方法—clampViewPositionVertical(垂直方向滑动)和clampViewPosition—Horizontal(水平方向滑动)

                                                                return 0为不移动,垂直方向top 水平方向left right

onViewReleased() 手指离开屏幕后实现的操作

mViewDragHelper.smoothSlideViewTo—相当于startScroll方法

onFinishInflate方法遍历获取子View


Tips:ViewDragHelper.Callback源码中(未用方法)  

onViewCaptured 在触摸到View回调

onViewDragStateChanged:在拖拽状态改变时回调   return:1.IDLE 空闲    2.DRAGGING 拖动  3.SETTLING 按住不动

onViewPositionChanged:在位置改变是回调,常用滑动时更改scale进行缩放。

代码:

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by YRC on 2017/10/20.
 */

public class DragViewGroup extends FrameLayout {
    private View mMainView,mMenuView;
    private ViewDragHelper mViewDragHelper;
    public DragViewGroup(@NonNull Context context) {
        super(context);
        initView();
    }
    public DragViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public DragViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        mViewDragHelper=ViewDragHelper.create(this,callback);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int mwidth=mMenuView.getMeasuredWidth();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mMenuView=getChildAt(0);
        mMainView=getChildAt(1);
    }

    /**
     * 检查提供给父视图的onInterceptTouchEvent的此事件是否应导致父级拦截触摸事件流。
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return mViewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mViewDragHelper.processTouchEvent(event);
        return true;
    }

    private ViewDragHelper.Callback  callback=new ViewDragHelper.Callback() {
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return mMainView==child;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            return 0;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (left<0){
                return 0;
            }else {
                return left;
            }

        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if (mMainView.getLeft()<500){
                mViewDragHelper.smoothSlideViewTo(mMainView,0,0);
                ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
            }else {
                mViewDragHelper.smoothSlideViewTo(mMainView,300,0);
                ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
            }
        }

    };

    @Override
    public void computeScroll() {
        if (mViewDragHelper.continueSettling(true)){
            ViewCompat.postInvalidateOnAnimation(this);
        }
        super.computeScroll();
    }

}

xml

<com.example.yrc.part5.DragViewGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" />
    FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_dark">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Main" />
    FrameLayout>
com.example.yrc.part5.DragViewGroup>


你可能感兴趣的:(代码库)