ViewDragHelper使用的一般步骤:

  1. 初始化DragHelper
    mDragHelper = ViewDragHelper.create(this, new MyDragCallBack());
  2. 拦截事件,并处理事件

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //2.1 拦截事件
        return mDragHelper.shouldInterceptTouchEvent(ev);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //2.2 分析处理事件
        mDragHelper.processTouchEvent(event);
        return true;
    }
    
  3. 再回掉中处理相关操作
  4. 处理平滑的回滚

    模仿android Drawlayout效果

    `java

public class SlideLayout extends FrameLayout{
private Context mContext;
private ViewDragHelper mDragHelper;
private View mSlidView;
private int mSlidViewWidth;

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

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

public SlideLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    init();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    //获取SlidingView的宽度
    mSlidViewWidth = mSlidView.getMeasuredWidth();
}

private void init() {
    if(getChildCount() > 0){
        mSlidView = getChildAt(getChildCount() - 1);
    }
    //1,初始化DragHelper
    mDragHelper = ViewDragHelper.create(this, new MyDragCallBack());
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    //2.1 拦截事件
    return mDragHelper.shouldInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    //2.2 分析处理事件
    mDragHelper.processTouchEvent(event);
    return true;
}

class MyDragCallBack extends ViewDragHelper.Callback{
    @Override
    public boolean tryCaptureView(View child, int pointerId) {
        Log.d("id",pointerId+"");
        //3. 判断当前捕获的view是否是需要执行拖拽的view
        return child == mSlidView;
    }

    @Override
    public int clampViewPositionHorizontal(View child, int left, int dx) {
        //处理水平方向的拖动,参数含义:child:当前拖拽的view;left:水平方向移动的距离; dx与前一阵的dx
        return left;
    }

    @Override
    public int clampViewPositionVertical(View child, int top, int dy) {
        //处理水平方向的拖动
        return super.clampViewPositionVertical(child, top, dy);
    }

    @Override
    public void onViewReleased(View releasedChild, float xvel, float yvel) {
        //View拖拽完成后执行的回掉方法
        super.onViewReleased(releasedChild, xvel, yvel);
        if (releasedChild.getLeft() < mSlidViewWidth*2/3) {
            mDragHelper.smoothSlideViewTo(releasedChild,0,0);
            ViewCompat.postInvalidateOnAnimation(SlideLayout.this);
        } else if (releasedChild.getLeft() >= mSlidViewWidth*2/3){
            mDragHelper.smoothSlideViewTo(releasedChild,mSlidViewWidth * 2/3,0);
            ViewCompat.postInvalidateOnAnimation(SlideLayout.this);
        }
     }

    @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 onViewDragStateChanged(int state) {
//拖拽状态改变时被回掉,如idle,fling
super.onViewDragStateChanged(state);
}
}

@Override
public void computeScroll() {
    super.computeScroll();
    //拖拽是否需要继续
    if(mDragHelper.continueSettling(true)){
        //正在执行中
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

}
`