WebView 如何同时拥有滑动和触摸事件?

How to have both swipe and touch events for WebView?

大家好,我正在创建 epub reader 并且我已将我的 WebView 自定义为水平滚动,现在我想停止默认滚动并根据我已经实现的滑动手势以编程方式滚动像这样

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new GestureListener());
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);

}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_DISTANCE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float distanceX = e2.getX() - e1.getX();
        float distanceY = e2.getY() - e1.getY();
        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceX > 0)
                onSwipeRight();
            else
                onSwipeLeft();
            return true;
        }
        return false;
    }

  }
 }

在我的主要 activity

 webView.setOnTouchListener(new OnSwipeTouchListener(this) {
                        @Override
                        public void onSwipeLeft() {
                            // my code to scroll previous page

                            }

                        }
                        @Override
                        public void onSwipeRight() {
                            //my code to scroll next page
                        }
                    });

现在问题已经说明了,我无法在长按选择文本时拥有 WebView 的默认功能,我怎样才能拥有滑动事件并在长按时仍然获得文本选择功能?

通过 onTouch() 的实现,ViewonTouchEvent() 方法将不会被调用,从而阻止其默认触摸行为 运行。

更改以下内容:

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

为此:

public boolean onTouch(View v, MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    return v.onTouchEvent(event);
}

如果你想停止默认的滚动操作,写这个:

gestureDetector.onTouchEvent(event);
if(event.getAction()== MotionEvent.ACTION_MOVE) {
    return true;
} else {
    return v.onTouchEvent(event);
}