开发者

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

开发者 https://www.devze.com 2023-02-14 22:25 出处:网络
I\'m still figuring out the ins and outs of the SDK so bear with me here. So i\'m trying to implement a Webview, that keeps multitouch zoom controls and vertical scroll intact (webview class handles

I'm still figuring out the ins and outs of the SDK so bear with me here.

So i'm trying to implement a Webview, that keeps multitouch zoom controls and vertical scroll intact (webview class handles them) while locking horizontal scroll on the Webview, and implementing horizontal flings to seek new data.

I found this: Fling Gesture and Webview in Android . I therefore took this code to understand how to implement this in my app later, so I'm working from there (the answer, of course).

And started modyfing the code to my suiting. For example:

   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");

Actually detects ONLY horizontal flings.

However, I have not figured out how to make sure that horizontal flings trigger an action of my choosing, while vertical flings still control the scroll of the WebView in question.

My first thought was to try something like pageUp/pageDown or scrollTo on the webview but that can't work since i'm extending the webview class to MyWebView and therefore don't yet have an instanciated object of this type.

This is the full code, if needed:

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开发者_如何学C = 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!


What you want to do here is add code to your onTouchEvent to check the return value from the GestureDetector's onTouchEvent, and if it is false, then call the WebView superclass with the event, like this:

<code>
    @Override 

    public boolean onTouchEvent(MotionEvent event) {

        return (gd.onTouchEvent(event) 
            || super.onTouchEvent(event)); 
    }  
</code>

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


I wrote the class which is extended from WebView and can detect the swipes easily.

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)); 
}  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消