Android 判断Webview 的水平滚动

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
 add comment

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值