ViewGroup 在我设置时仍然可以滚动,当 getScrollX()>=0 时可以滚动



下面是我的代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x=(int)event.getX();
if (action == MotionEvent.ACTION_DOWN) {
mLastX = x;
mStart = getScrollX();
} else if (action == MotionEvent.ACTION_MOVE) {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
Log.d(TAG, "onTouchEvent: getScrollX-->"+getScrollX());
int dx=mLastX-x;
if(getScrollX()>=0&&getScrollX()<mSlideViewWidth){
scrollBy(dx,0);
}
mLastX=x;
} else if (action == MotionEvent.ACTION_UP) {
mEnd = getScrollX();
if(getScrollX()<0){
mScroller.startScroll(getScrollX(),0,-mEnd,0);
}
}
postInvalidate();
return true;
}

当我向左滑动屏幕时,ScrollX小于0,因此不应该调用scrollby方法。但当我向左滑动屏幕时,ViewGroup仍然可以向左移动一小段距离。我猜解决方案可能与getScrollX方法有关。但因为我最近才学习自定义视图,我不知道如何处理这个。我在网上找了很长时间。但是没有用。请帮助或尝试给一些想法如何实现这一目标。提前感谢。问题描述gif

这是一个动画问题,或者你有一个翻译效果仍然在最后一个scrollX之前,或者可能你的情况,dx值+getScrollX()在你的最后一次迭代大于0,你的解决方案是看看你是否接近边缘,并调整dx-getScrollX()的确切数量,如果它更大。

int dx=mLastX-x;
if (dx < -getScrollX()) dx = -getScrollX(); //not tested but should work
if(getScrollX()>=0&&getScrollX()<mSlideViewWidth)
scrollBy(dx,0);

最新更新