如何同时为WebView滑动和触摸事件? [英] How to have both swipe and touch events for WebView?
问题描述
大家好,我正在创建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()
,将不会调用View
的onTouchEvent()
方法,从而阻止其运行默认的触摸行为.>
更改以下内容:
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);
}
Hello guys i am creating epub reader and i have customised my WebView
to scroll horizontal, now i want to stop the default scroll and do scrolling programatically based on swipe guestures i have already acheive it like this way
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;
}
}
}
and in my main 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
}
});
now the problem stated, i cannot have the default feature of WebView
on longpress selecting text, how can i have swiping events and still get the text selection feature on long press?
By your implementation of onTouch()
, the onTouchEvent()
method of the View
will not be called, preventing its default touch behaviour to run.
Change the following:
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);
}
这篇关于如何同时为WebView滑动和触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!