我有一个问题,要触摸“ webView
”时使用多页水平方式触摸。如何在我们的项目中实现这一点?请建议我以获得正确的结果。
答案 0 :(得分:0)
只需将简单的OnTouchListener添加到您的Web视图。单击链接,滚动仍然有效。
只需遵循3个简单步骤:
1-使您的调用对象实现一个简单的TouchListener:
public interface TouchListener {
default void onSwipeLeft() {
Logger.d( "Swipe left default action");
}
default void onSwipeRight() {
Logger.d( "Swipe right default action");
}
}
2-在您的调用类中,将以下OnTouchListener添加到您的webView中:
webView.setOnTouchListener( new OnSwipeWebviewTouchListener( getActivity(), this));
使用您的功能实现onSwipeLeft()和onSwipeRight()方法。
3-使用此OnTouchListener:
public class OnSwipeWebviewTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeWebviewTouchListener(Context ctx, TouchListener touchListener) {
gestureDetector = new GestureDetector(ctx, new GestureListener(touchListener));
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private TouchListener touchListener;
GestureListener(TouchListener touchListener) {
super();
this.touchListener = touchListener;
}
@Override
public boolean onDown(MotionEvent e) {
return false; // THIS does the trick
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
// You can customize these settings, so 30 is an example
if (Math.abs(diffX) > 30 && Math.abs(velocityX) > 30) {
if (diffX > 0) {
touchListener.onSwipeRight();
} else {
touchListener.onSwipeLeft();
}
result = true;
}
} else if (Math.abs(diffY) > userSettings.swipeYDistanceThreshold && Math.abs(velocityY) > userSettings.swipeYSpeedThreshold) {
result = false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
}