How to override some gesture events over a webview, but allow others?

I'm still good at SDK attachments and outputs, so carry me here.

So, I'm trying to implement Webview, which keeps multitouch zoom controls and vertical scrolling intact (the webview class handles them), while simultaneously blocking horizontal scrolling in Webview and using horizontal throws to find new data.

I found this: Fling Gesture and Webview on Android , so I adopted this code to figure out how to implement this in my application later, so I work from there (answer of course).

And he began to modify the code in accordance with my purpose. For instance:

if (event1.getRawX() > event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100) { show_toast("swipe left"); } else if(event1.getRawX() < event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100){ show_toast("swipe right"); 

In fact, it detects ONLY horizontal outliers.

However, I did not understand how to make sure that horizontal clicks trigger the action of my choice, while vertical throws still control the scrolling of the WebView in question.

My first thought was to try something like pageUp / pageDown or scrollTo in a web browser, but that would not work, since I am extending the webview class to MyWebView and therefore have not yet created a substring object of this type.

This is the complete code, if necessary:

 package test.fling; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.webkit.WebView; import android.widget.Toast; public class testicules extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyWebView webview = new MyWebView(this); webview.loadUrl("http://en.wikipedia.org/wiki/Android"); setContentView(webview); } class MyWebView extends WebView { Context context; GestureDetector gd; public MyWebView(Context context) { super(context); this.context = context; gd = new GestureDetector(context, sogl); } @Override public boolean onTouchEvent(MotionEvent event) { return gd.onTouchEvent(event); } GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() { public boolean onDown(MotionEvent event) { return true; } public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { if (event1.getRawX() > event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100) { show_toast("swipe left"); } else if(event1.getRawX() < event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100){ show_toast("swipe right"); } else { //MyWebView.pageUp(true); /*can't work, as explained above*/ } return true; } }; void show_toast(final String text) { Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT); t.show(); } } } 

Any help would be greatly appreciated. Thanks!

+4
source share
2 answers

What you want to do is add code to your onTouchEvent to check the return value from the GestureDetector onTouchEvent, and if it is false, then call the superclass of the WebView class with an event, for example:

 <code> @Override public boolean onTouchEvent(MotionEvent event) { return (gd.onTouchEvent(event) || super.onTouchEvent(event)); } </code> 

Then your onFling method can return true if you are handling the event, or false if you want WebView to handle the event. You also need to change onDown to return false, so WebView gets a chance at the start of the down event.

+7
source

I wrote a class that extends from WebView and can easily detect swipes.

 import android.content.Context; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.GestureDetector.SimpleOnGestureListener; import android.webkit.WebView; import android.widget.Toast; public class HtmlImageView extends WebView { Context mContext; GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector()); public HtmlImageView(Context context) { super(context); mContext=context; } public HtmlImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext=context; } public HtmlImageView(Context context, AttributeSet attrs) { super(context, attrs); mContext=context; } private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return super.onScroll(e1, e2, distanceX, distanceY); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; // right to left swipe if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(mContext, "Left Swipe", Toast.LENGTH_SHORT) .show(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(mContext, "Right Swipe", Toast.LENGTH_SHORT) .show(); } } catch (Exception e) { // nothing } return false; } } public boolean onTouchEvent(MotionEvent event) { return (gestureDetector.onTouchEvent(event) || super.onTouchEvent(event)); } } 
+1
source

Source: https://habr.com/ru/post/1342041/


All Articles