完美解决横向纵向滑动冲突

来源:互联网 发布:asp php jsp的优缺点 编辑:程序博客网 时间:2024/05/17 23:08

很简单,自定义父控件在重写onTouchEvent 便可,代码如下。屡试不爽。

代码也很简单如果是横向滑动,直接返回false即不竖向滑动。


@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
if (xDistance > yDistance)
return false;
}


return super.onInterceptTouchEvent(ev);
}
0 0
原创粉丝点击