private ViewDragHelper mViewDragHelper;
private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
* @return 返回true表示获得view的控制权
*/
@Override
public boolean tryCaptureView(View view, int i) {
return view == contentView || view == deleteView;
}
@Override
public void onViewCaptured(View capturedChild, int activePointerId) {
super.onViewCaptured(capturedChild, activePointerId);
}
* 控制view在水平方向上实际滑动了多少
* @param child 当前触摸的view
* @param left view的左边坐标,负数表示view的左边超出父view边界的长度
* @param dx
* @return 返回多少,代表想让child的left=多少
*/
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
Log.i(TAG, "clampViewPositionHorizontal-->" + "left=" + left);
if (child == contentView) {
if (left > 0) left = 0;
if (left < -deleteViewWidth) left = -deleteViewWidth;
} else if (child == deleteView) {
if (left < contentViewWidth - deleteViewWidth) {
left = contentViewWidth - deleteViewWidth;
}
}
return left;
}
* 水平方向拖拽的范围
*/
@Override
public int getViewHorizontalDragRange(View child) {
return super.getViewHorizontalDragRange(child);
}
* view滑动后的回调
* @param changedView
* @param left
* @param top
* @param dx x轴方向的改编值
* @param dy
*/
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
Log.i(TAG, "onViewPositionChanged-->" + "dx=" + dx);
if (changedView == contentView) {
deleteView.layout(deleteView.getLeft() + dx, 0, deleteView.getRight() + dx, deleteView.getBottom());
} else if (changedView == deleteView) {
contentView.layout(contentView.getLeft() + dx, 0, contentView.getRight() + dx, contentView.getBottom());
}
}
* TouchUp的回调
* @param releasedChild
* @param xvel
* @param yvel
*/
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if (contentView.getLeft() < -deleteViewWidth / 2) {
mViewDragHelper.smoothSlideViewTo(contentView, -deleteViewWidth, 0);
} else {
mViewDragHelper.smoothSlideViewTo(contentView, 0, 0);
}
ViewCompat.postInvalidateOnAnimation(SwipeView.this);
}
};