2013-04-25 80 views
1

我使用ViewPager Extension使我的應用程序具有選項卡功能。在我的應用程序中有兩個選項卡,我們在這裏命名爲tabA和tabB。 tabA包含一個webview組件,tabB包含一個listview。防止在滾動時在標籤的webview中點擊事件

現在,我遇到了一個問題: 一旦我嘗試從tabA滾動到tabB,tabA中的頁面在更改爲tabB時(或之前)發生更改。換句話說,當我嘗試從tabA滾動到tabB時,我的要求是不會觸發tabA中的click事件。

有沒有人有任何想法或經驗的問題,非常感謝!

我試圖使用GestureDetector來檢測手勢,但它不適用於我。

final GestureDetector dector = new GestureDetector(new OnGestureListener(){...} 
webView.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     return dector.onTouchEvent(event); 
    } 
}); 

回答

0

你可以做的是什麼

  1. 檢測刷卡並設置一個標誌,那麼

    public interface TouchDelegate { 
        public void gestureHandler(String msg, float downX, float downY); 
    
        public boolean notASwipe(View v, MotionEvent event); 
    } 
    
    public class SwipeDetector implements View.OnTouchListener { 
    
        private TouchDelegate mView; 
        static final int MIN_DISTANCE = 50; 
        private float downX, downY, upX, upY; 
        public SwipeDetector(TouchDelegate view) { 
         this.mView = view; 
        } 
    
        public void onRightToLeftSwipe() { 
         mView.gestureHandler("R2L", downX, downY); 
        } 
    
        public void onLeftToRightSwipe() { 
         mView.gestureHandler("L2R", downX, downY); 
        } 
    
        public void onTopToBottomSwipe() { 
         mView.gestureHandler("T2B", downX, downY); 
        } 
    
        public void onBottomToTopSwipe() { 
         mView.gestureHandler("B2T", downX, downY); 
        } 
    
        public boolean onTouch(View v, MotionEvent event) { 
        switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: { 
         downX = event.getX(); 
         downY = event.getY(); 
         return true; 
        } 
        case MotionEvent.ACTION_UP: { 
         upX = event.getX(); 
         upY = event.getY(); 
    
         float deltaX = downX - upX; 
         float deltaY = downY - upY; 
    
         // swipe horizontal? 
         if (Math.abs(deltaX) > MIN_DISTANCE) { 
          // left or right 
          if (deltaX < 0) { 
           this.onLeftToRightSwipe(); 
           return true; 
          } 
          if (deltaX > 0) { 
           this.onRightToLeftSwipe(); 
           return true; 
          } 
         } else { 
          // swipe vertical? 
          if (Math.abs(deltaY) > MIN_DISTANCE) { 
           // top or down 
           if (deltaY < 0) { 
            this.onTopToBottomSwipe(); 
            return true; 
           } 
           if (deltaY > 0) { 
            this.onBottomToTopSwipe(); 
            return true; 
           } 
          } else { 
           Log.d(getClass().getSimpleName(), 
             "SwipeDetector: onTouch: Swipe was only x: " + Math.abs(deltaX) 
              + " long, need at least " + MIN_DISTANCE); 
          Log.d(getClass().getSimpleName(), 
            "SwipeDetector: onTouch: Swipe was only y: " 
              + Math.abs(deltaY) 
              + " long, need at least " + MIN_DISTANCE); 
          return mView.notASwipe(v, event); // We don't consume the 
                   // event 
         } 
        } 
        return true; 
    } 
    } 
    return false; 
    } 
    } 
    
  2. 覆蓋的shouldOverrideUrlLoading來禁用web視圖的所有環節。這裏是禁止在web視圖所有的鏈接代碼:

    public class MyWebViewClient extends WebViewClient { 
    public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) { 
        if(swipe)    
         return true; 
        return false; 
    } 
    public boolean shouldOverrideUrlLoading (WebView view, String url) { 
        if (!swipe) { 
         view.loadUrl(url); 
        } 
        return true; 
    } 
    } 
    
+0

謝謝你的回答。但我仍不清楚應該在哪裏設置swip值。另外,我只想在滾動期間禁用而不是禁用webview上的所有鏈接。 – Weibo 2013-04-25 02:56:15

+0

我添加了需要檢測刷卡的代碼,基本上你的視圖應該實現TouchDelegate,然後將視圖的onTouchListener設置爲swipedetector類的一個實例! – 2013-04-25 04:06:01

+0

我剛剛嘗試過,但很不幸。我試着如下:'webView.setOnTouchListener(new OnTouchListener(){@Override public boolean onTouch(View v,MotionEvent event){swipe = dector.onTouch(v,event); return swipe;}}); '另一方面,當向上/向下滾動時,外觀webview不會觸發點擊事件。你知道webview如何控制它嗎? – Weibo 2013-04-25 15:26:25

0

子類WebView,然後重寫onTouchEvent方法是這樣的:

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    // If this web view gets sent a 'CANCEL' event, it means the 
    // parent view has intercepted the event (for scrolling/swiping), 
    // so we want to catch it, otherwise it propagates into the HTML 
    // and triggers the click 
    int action = MotionEventCompat.getActionMasked(event); 
    if(action == MotionEvent.ACTION_CANCEL) 
    { 
     return true; 
    } 
    return super.onTouchEvent(event); 
} 

什麼應該發生的是,封閉ViewPager當存在正確的滑動時已經攔截了觸摸事件,這會將ACTION_CANCEL事件發送到WebView。通過捕獲onTouchEvent方法中的ACTION_CANCEL事件,它可以防止事件進入WebView中的HTML。

0

我有同樣的問題。我已經搜索了一些需要用java編碼的想法,但我認爲這不是一個好的,簡單的,乾淨的方法。然後我發現了一個非常簡單和乾淨的解決方案。您只需使用此行android:descendantFocusability="blocksDescendants"在webView的父代中使用此行代碼。那就是它