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

2024-02-16

大家好,我正在创建 epub 阅读器,并且我已经定制了我的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;
    }

  }
 }

在我的主要活动中

 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(), the onTouchEvent()的方法View不会被调用,从而阻止其默认触摸行为运行。

更改以下内容:

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

To this:

public boolean onTouch(View v, MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    return v.onTouchEvent(event);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让WebView同时具有滑动和触摸事件? 的相关文章

随机推荐