public class SwipeLayout extends LinearLayout {
private ViewDragHelper viewDragHelper;
private View contentView;
private View actionView;
private int dragDistance;
private final double AUTO_OPEN_SPEED_LIMIT = 500.0;
private int draggedX;
private float startX = 0;
private float startY = 0;
private float curX = 0;
private float curY = 0;
private boolean isMove = false;
public SwipeLayout(Context context) {
this(context, null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
viewDragHelper = ViewDragHelper.create(this, new DragHelperCallback());
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
contentView = getChildAt(0);
actionView = getChildAt(1);
actionView.setVisibility(GONE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
dragDistance = actionView.getMeasuredWidth();
}
private class DragHelperCallback extends ViewDragHelper.Callback {
@Override
public boolean tryCaptureView(View view, int i) {
return view == contentView || view == actionView;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
draggedX = left;
if (changedView == contentView) {
actionView.offsetLeftAndRight(dx);
} else {
contentView.offsetLeftAndRight(dx);
}
if (actionView.getVisibility() == View.GONE) {
actionView.setVisibility(View.VISIBLE);
}
invalidate();
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == contentView) {
final int leftBound = getPaddingLeft();
final int minLeftBound = -leftBound - dragDistance;
final int newLeft = Math.min(Math.max(minLeftBound, left), 0);
return newLeft;
} else {
final int minLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() - dragDistance;
final int maxLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() + getPaddingRight();
final int newLeft = Math.min(Math.max(left, minLeftBound), maxLeftBound);
return newLeft;
}
}
@Override
public int getViewHorizontalDragRange(View child) {
return dragDistance;
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
boolean settleToOpen = false;
if (xvel > AUTO_OPEN_SPEED_LIMIT) {
settleToOpen = false;
} else if (xvel < -AUTO_OPEN_SPEED_LIMIT) {
settleToOpen = true;
} else if (draggedX <= -dragDistance / 2) {
settleToOpen = true;
} else if (draggedX > -dragDistance / 2) {
settleToOpen = false;
}
final int settleDestX = settleToOpen ? -dragDistance : 0;
viewDragHelper.smoothSlideViewTo(contentView, settleDestX, 0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (viewDragHelper.shouldInterceptTouchEvent(ev)) {
return true;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
viewDragHelper.processTouchEvent(event);
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
//拒绝父类View进行拦截
disallowParentsInterceptTouchEvent(getParent());
startX = ev.getX();
startY = ev.getY();
isMove = false;
break;
case MotionEvent.ACTION_MOVE:
if (!isMove) {
curX = ev.getX();
curY = ev.getY();
float dx = curX - startX;
float dy = curY - startY;
//设置判断是否移动矩形框
if (dx * dx + dy * dy > 50 * 50) {
//当用户进行横向操作时,才进行拦截
if (Math.abs(dx) > Math.abs(dy)) {
isMove = true;
} else {
//允许父类布局对该布局进行拦截
allowParentsInterceptTouchEvent(getParent());
}
} else {
//释放拦截
....
}
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
private void disallowParentsInterceptTouchEvent(ViewParent parent) {
if (null == parent) {
return;
}
parent.requestDisallowInterceptTouchEvent(true);
disallowParentsInterceptTouchEvent(parent.getParent());
}
private void allowParentsInterceptTouchEvent(ViewParent parent) {
if (null == parent) {
return;
}
parent.requestDisallowInterceptTouchEvent(false);
allowParentsInterceptTouchEvent(parent.getParent());
}
@Override
public void computeScroll() {
super.computeScroll();
if (viewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
}
在运行中发现一个问题,滑动冲突是没有了,但是父布局的onClick方法不能用了,导致了没有办法点击。
因为该布局写在ListView中,自定义一个设置拦截的ListView又太麻烦。所以处理方式是将onClick方法替换成onTouch()方法。
View.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
startX = motionEvent.getX();
startY = motionEvent.getY();
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
curX = motionEvent.getX();
curY = motionEvent.getY();
float dx = curX - startX;
float dy = curY - startY;
if (dx * dx + dy * dy < 50 * 50) {
if (!isInSet){
..............
}
}
return false;
}
});