检测WebView null手势异常的刷卡

时间:2014-05-10 08:28:02

标签: android webview swipe

我正在努力在webview上添加滑动动作,但是当涉及到执行时,除了Gesture Detection变量为空之外。你能不能告诉我,我有什么能够初步确定手势检测器?以下是我的代码

申请部分

wv = (CustomWebView)this.findViewById(R.id.webViewX);
    //wv = new CustomWebView(this);
    WebSettings settings = wv.getSettings();
    settings.setDefaultTextEncodingName("UTF-8");
    //settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    //settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    settings.setBuiltInZoomControls(true);

自定义网页浏览:

public final class CustomWebView extends WebView {

    private GestureDetector gestureDetector;
    private WebView webview;
    private float currentScale;
    private Context ctx;
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomWebView(Context context) {
        super(context);
        this.webview = this;
        ctx = context;
        gestureDetector = new GestureDetector(ctx , new CustomeGestureDetector());
        this.setGestureDetector(gestureDetector);   

    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWebViewClient(new WebViewClient() {
            @Override
            public void onScaleChanged(WebView view, float oldScale, float newScale) {
                super.onScaleChanged(view, oldScale, newScale);
                currentScale = newScale;
            }
        });
    }

    /**
     * @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;
    }

    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
                        String url = "http://www.stackoverflow.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url)); 
                        ctx.startActivity(i); 
                        return true;
                    } //left to right swipe .. go to prev page
                    else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) {
                        //do your stuff
                        String url = "http://www.google.com";
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url)); 
                        ctx.startActivity(i); 
                        return true;
                    } //bottom to top, go to next document
                    else if(e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 800 
                            && webview.getScrollY() >= currentScale * (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;
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我在WebViews上实现了手势检测。可能会帮助你:

http://vshivam.wordpress.com/2013/09/02/privly-reading-app-integration-and-detecting-swipe-gesture/

检查一下。这可能是检测滑动方向的更好方法恕我直言:http://vshivam.wordpress.com/2014/04/28/detecting-up-down-left-right-swipe-on-android/