外部拦截法解决滑动冲突
声明成员变量:
/** * 处理滑动冲突的属性 */ private int mLastIntercepX = 0; private int mLastIntercepY = 0;
//外部拦截法处理滑动冲突 public boolean onInterceptTouchEvent(MotionEvent event) { boolean intercept = false; int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: intercept = false; break; case MotionEvent.ACTION_MOVE: int deltaX= (int) (x-mLastIntercepX); int deltaY= (int) (y-mLastIntercepY); if(Math.abs(deltaX)>Math.abs(deltaY)){//左右滑动 intercept = true; } else {//上下滑动,交给子View处理 intercept = false; } break; case MotionEvent.ACTION_UP: intercept = false; break; } mLastIntercepX = x; mLastIntercepY = y; return intercept; }
时间: 2024-10-31 21:57:19