简体   繁体   English

带有手势的WebView

[英]WebView with swipe Gesture

I have a page with its first part containing animations. 我有一个页面,其第一部分包含动画。 when the user swipes, the animation goes and a webview appears. 当用户滑动时,动画消失并且出现Web视图。 On swipe up, the webview goes and the animations again appears. 向上滑动时,Web视图将出现,并且动画再次出现。 since the webview was consuming the touch, I am overriding the webview touch and passing it to gesturedetector object. 由于Webview正在消耗触摸,因此我要重写Webview触摸并将其传递给手势检测器对象。 But what I really want is the control to be switched between webview and the gesturedetector. 但是我真正想要的是在Web视图和手势检测器之间切换的控件。 For now the gesturedetector works or the webview works but it doesnt work together. 目前,手势检测器可以工作或Web视图可以工作,但不能一起工作。 Any help? 有什么帮助吗?

This is what I am doing now: 这就是我现在正在做的:

    webView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            return gestureDetector.onTouchEvent(event);
            // return false;

        }
    });

The gesture detector handles the swiping between animations and webview: 手势检测器处理动画和Web视图之间的滑动:

    gestureDetector = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener() {
                /* Function to pause the video on tap */
                @Override
                public boolean onSingleTapConfirmed(MotionEvent e) {
                    /* On Touch event pressed play and pause the videoplayer */
                    return true;
                }

                /*
                 * OnDown() has to return true for the fling methof to take
                 * place
                 */
                @Override
                public boolean onDown(MotionEvent e) {
                    return true;
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2,
                        float velocityX, float velocityY) {

Use a GestureDetector with a custom web view.. GestureDetector与自定义Web视图一起使用。

webView.setGestureDetector(new GestureDetector(new CustomeGestureDetector())); webView.setGestureDetector(new GestureDetector(new CustomeGestureDetector()));

gesture detector : 手势检测器

private class CustomeGestureDetector extends SimpleOnGestureListener {      
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if(e1 == null || e2 == null) return false;
        if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
        else {
            try { // right to left swipe .. go to next page
                if(e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 800) {
                    //do your stuff
                    return true;
                } //left to right swipe .. go to prev page
                else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) {
                    //do your stuff
                    return true;
                } //bottom to top, go to next document
                else if(e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 800 
                        && webView.getScrollY() >= webView.getScale() * (webView.getContentHeight() - webView.getHeight())) {
                    //do your stuff
                    return true;
                } //top to bottom, go to prev document
                else if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 800 ) {
                    //do your stuff
                    return true;
                } 
            } catch (Exception e) { // nothing
            }
            return false;
        }
    }
}

custom web view 自定义网页视图

public final class CustomWebView extends WebView {

private GestureDetector gestureDetector;

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public CustomWebView(Context context) {
    super(context);
}

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public CustomWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/* 
 * @see android.webkit.WebView#onScrollChanged(int, int, int, int)
 */
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
}

/* 
 * @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
 */
@Override
public boolean onTouchEvent(MotionEvent ev) {
    return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}

public void setGestureDetector(GestureDetector gestureDetector) {
    this.gestureDetector = gestureDetector;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM