Android 判斷Webview 的水平滾動

來源:互聯網
上載者:User

I found a solution which work for me, I check the source code of class WebView in 2.3 API and find how to do it with a 2.1 API. Maybe it can work with older 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);    }}
share|edit|flag answered
Jan 14 '11 at 19:57

Carbodrache
7619
  add comment

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.