ViewDragHelper的使用
ViewDragHelper viewDragHelper = ViewDragHelper.create(this, callback);
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return viewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
viewDragHelper.processTouchEvent(event);
return true;
}
ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
//捕获那个视图
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
//捕获方向上的范围
@Override
public int getViewHorizontalDragRange(View child) {
return 10;
}
//限制方向上的位置
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return super.clampViewPositionHorizontal(child, left, dx);
}
//当位置改变调用
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
}
//松手是调用
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
}
};