2013-05-06 61 views
0

我有一個頁面,其第一部分包含動畫。當用戶滑動時,動畫將會出現並顯示一個web視圖。在向上滑動時,網頁視圖將會出現,並且動畫再次出現。由於webview消耗了觸摸,我重寫了webview touch並將其傳遞給了gesturedetector對象。但我真正想要的是在webview和gesturedetector之間切換控件。現在,手勢檢測器的工作原理或Web視圖的工作原理,但它並不一起工作。任何幫助?帶有滑動手勢的WebView

這就是我現在所做的:

webView.setOnTouchListener(new View.OnTouchListener() { 

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

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

     } 
    }); 

姿態探測器處理動畫和網頁視圖之間的重擊:

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) { 

回答

1

使用帶有自定義Web視圖GestureDetector ..

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

姿態探測器

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

自定義Web視圖

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

感謝您的回答。我見過這個例子。滑動工作,但控制不來webview。有什麼條件我必須檢查? – 2013-05-06 05:45:55

+0

webview.getSettings()。setBuiltInZoomControls(true); – 2013-05-06 05:47:57

+0

我不想要縮放控件。我只想將滑動事件發送到手勢檢測器,以便在onfling()方法內處理它。 – 2013-05-06 07:14:40