2011-01-10 79 views
7

我需要知道如何檢測用戶是否無法在webView中滾動。我想,當用戶刷卡向左或向右產生作用,但前提是用戶不能滾動:Android webview:檢測滾動

IF the user swipes to the left AND the webview can't scroll to left THEN 
do something 
ELSE 
let the webview scroll 

回答

11

我找到了一個適合我的解決方案,我在2.3 API中檢查了類WebView的源代碼,並找到了如何使用2.1 API進行操作。也許它可以與舊的API工作:

public class CustomWebView extends WebView { 

    private float oldX; 

    // indicate if horizontal scrollbar can't go more to the left 
    private boolean overScrollLeft = false; 

    // indicate if horizontal scrollbar can't go more to the right 
    private boolean overScrollRight = false; 

    // indicate if horizontal scrollbar can't go more to the left OR right 
    private boolean isScrolling = false; 

    public CustomWebView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 


    public CustomWebView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 



    public CustomWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // width of the vertical scrollbar 
     int scrollBarWidth = getVerticalScrollbarWidth(); 

     // width of the view depending of you set in the layout 
     int viewWidth = computeHorizontalScrollExtent(); 

     // width of the webpage depending of the zoom 
     int innerWidth = computeHorizontalScrollRange(); 

     // position of the left side of the horizontal scrollbar 
     int scrollBarLeftPos = computeHorizontalScrollOffset(); 

     // position of the right side of the horizontal scrollbar, the width of scroll is the width of view minus the width of vertical scrollbar 
     int scrollBarRightPos = scrollBarLeftPos + viewWidth - scrollBarWidth; 

     // if left pos of scroll bar is 0 left over scrolling is true 
     if(scrollBarLeftPos == 0) { 
      overScrollLeft = true; 
     } else { 
      overScrollLeft = false; 
     } 

     // if right pos of scroll bar is superior to webpage width: right over scrolling is true 
     if(scrollBarRightPos >= innerWidth) { 
      overScrollRight = true; 
     } else { 
      overScrollRight = false; 
     } 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: // when user touch the screen 
      // if scrollbar is the most left or right 
      if(overScrollLeft || overScrollRight) { 
       isScrolling = false; 
      } else { 
       isScrolling = true; 
      } 
      oldX = event.getX(); 
      break; 

     case MotionEvent.ACTION_UP: // when user stop to touch the screen 
      // if scrollbar can't go more to the left OR right 
      // this allow to force the user to do another gesture when he reach a side 
      if(!isScrolling) { 
       if(event.getX() > oldX && overScrollLeft) { 
        // left action 
       } 

       if(event.getX() < oldX && overScrollRight) { 
        // right actio 
       } 
      } 

      break; 
     default: 
      break; 
     } 
     return super.onTouchEvent(event); 
    } 

} 
+0

當內容放大或縮小時,這不起作用。任何解決方案 – Chitrang

3

有API中的一些有趣的方法,但它們受到保護,我想你可以創建自己的WebView(擴展WebView)並使用這些方法並將其公開化。 具體來說,也許你可以使用這兩個 onScrollChangedonOverScroll

+0

的onOverScroll方法是Android 2.3的,所以我會得到的WebView的源代碼,2.3和包括它在我的項目中。 – Carbodrache

+0

很高興知道!祝你好運 – mdelolmo

0

我認爲這將解決這個問題:

int horizontalOffset = computeHorizontalScrollOffset(); 
int contentWidth = (int) Math.floor(getContentWidth() * getScale()); 
int viewportWidth = getWidth(); 

// 1 just for random error 
boolean reachRightEnd = ((horizontalOffset + viewportWidth) >= (contentWidth - 1)); 
boolean reachLeftEnd = (horizontalOffset == 0);