微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android webview:检测滚动

我需要知道如何检测用户是否无法在webView中滚动.我想在用户向左或向右滑动时生成一个动作,但仅当用户无法滚动时:
IF the user swipes to the left AND the webview can't scroll to left THEN
 do something
ELSE
 let the webview scroll

解决方法

我找到了一个适合我的解决方案,我在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);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐