我有Webview,当我触摸向左或向右移动时,页面应该移动,但是当我向下滚动时,它不应移动到下一页。 向下滑动时如何限制网页不移动。问题出在onFling上。因为当我向左或向右滑动时,它会打印正确并移至下一页。这可以。但是,当我向下滑动或向下滑动时,它将移至下一页。如何限制呢?
private boolean mControlEnabled = true;
private final GestureDetector.OnGestureListener mOnGestureListener =
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(final MotionEvent e) {
performClick();
return super.onSingleTapUp(e);
}
@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2,
final float velocityX, final float velocityY) {
if (isControlEnabled() && (((int) velocityX) != 0) && !mFlinging) {
Log.i("isControlEnabled", "onFling: "+isControlEnabled());
mFlinging = true;
if (velocityX < 0) {
incrementToNextSegment(mTouchDownOffset);
}
else {
decrementToPreviousSegment(mTouchDownOffset);
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
};
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (isOkayToSnap() && (l != oldl)) {
if (mScrollState == ScrollState.NONE) {
mScrollState = ScrollState.OTHER;
resetSnap();
}
else {
removeCallbacks(mScrollDetector);
}
postDelayed(mScrollDetector, SCROLL_DETECTION_TIMEOUT);
}
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (isOkayToSnap()) {
snap();
}
}
private void handleFlingOnTouch(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
mFlinging = false;
break;
}
}
private boolean snap() {
return snap(computeHorizontalScrollOffset());
}
private synchronized boolean snap(int offset) {
boolean returnValue = false;
if (isOkayToSnap()) {
int snapPosition = computeSnapPositionForOffset(offset);
if (snapPosition >= 0) {
resetSnap();
mSmoothScroller.start(offset, snapPosition);
returnValue = true;
}
}
return returnValue;
}