更新时间:2023-11-17 16:30:58
我遇到了同样的问题,通过在 RealViewSwitcher 中拦截触摸事件找到了解决方案.
I faced with the same problem and found solution by intercepting touch event in RealViewSwitcher.
之前发布的变体不适用于较新的 API.代码有第三次修订.
previous posted variant does not work on newer API. There is third revision of code.
将这些行添加到 RealViewSwitcher 实现中:
Add those lines into RealViewSwitcher implementation:
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// prevent vertical scrolling when view switching in progress
if (!mScroller.isFinished())
return true;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mScrollLocked = false;
mLastX = event.getX();
mLastY = event.getY();
mDeltaX = 0;
mDeltaY = 0;
onTouchEvent(event);
break;
case MotionEvent.ACTION_MOVE:
final float x = event.getX();
final float y = event.getY();
mDeltaX += Math.abs(x - mLastX);
mDeltaY += Math.abs(y - mLastY);
mLastX = x;
mLastY = y;
if (mDeltaX > mDeltaY) {
mScrollLocked = true;
onTouchEvent(event);
} else {
snapToDestination();
}
break;
}
if (mScrollLocked)
return true; // prevent furhter processing
return super.onInterceptTouchEvent(event);
}
还需要编辑onTouchEvent
方法并替换case MotionEvent.ACTION_DOWN:
body如下:
also need to edit onTouchEvent
method and replace case MotionEvent.ACTION_DOWN:
body as follows:
case MotionEvent.ACTION_DOWN:
/*
* If being flinged and user touches, stop the fling. isFinished will be false if being flinged.
*/
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
if (mTouchState == TOUCH_STATE_SCROLLING) {
mScroller.abortAnimation();
}
// Remember where the motion event started
mLastMotionX = x;
break;