【问题标题】:Android webview : detect scrollAndroid webview:检测滚动
【发布时间】:2011-01-10 07:39:47
【问题描述】:

我需要知道如何检测用户是否无法再在 webView 中滚动。我想在用户向左或向右滑动时生成一个动作,但前提是用户不能滚动:

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

【问题讨论】:

    标签: android webview scroll


    【解决方案1】:

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

    【讨论】:

    • 当内容被放大或缩小时,这不起作用。有什么解决办法吗?
    【解决方案2】:

    API 中有一些有趣的方法,但它们是受保护的,我认为您可以创建自己的 WebView(扩展 WebView)并使用这些方法并将它们公开。 具体来说,也许你可以使用这两个 onScrollChangedonOverScroll

    【讨论】:

    • onOverScroll 方法是针对android 2.3 的,所以我将获取2.3 的webview 的源代码并将其包含在我的项目中。
    【解决方案3】:

    我认为这会解决这个问题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2021-06-29
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多