本文介绍了机器人的WebView:检测滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道如何检测,如果用户不能在web视图不再滚动。我想,当用户扫描到左边或右边,以产生一个动作,但只有在用户不能滚动:
如果用户扫描的左侧和web视图不能滚动到左,那么
做一点事
其他
让web视图滚动
解决方案
我发现这对我的工作,我检查类的WebView源$ C $ C在2.3 API,并找到如何与2.1 API做一个解决方案。也许它可以与旧的API工作:
公共类CustomWebView扩展的WebView {
私人浮动oldX;
//说明是否水平滚动条不能走得更向左
私人布尔overScrollLeft = FALSE;
//说明是否水平滚动条不能去到右侧
私人布尔overScrollRight = FALSE;
//说明是否水平滚动条不能去更多地向左或向右
私人布尔isScrolling = FALSE;
公共CustomWebView(上下文的背景下){
超(上下文);
// TODO自动生成构造函数存根
}
公共CustomWebView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
// TODO自动生成构造函数存根
}
公共CustomWebView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
// TODO自动生成构造函数存根
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
//宽的垂直滚动条
INT scrollBarWidth = getVerticalScrollbarWidth();
//宽取决于你的观点的设置布局
INT viewWidth = computeHorizontalScrollExtent();
//宽度网页的根据变焦的
INT innerWidth = computeHorizontalScrollRange();
//水平滚动条左侧的位置
INT scrollBarLeftPos = computeHorizontalScrollOffset();
//右侧水平滚动条的位置时,涡旋的宽度的视图宽度减去垂直滚动条的宽度
INT scrollBarRightPos = scrollBarLeftPos + viewWidth - scrollBarWidth;
//如果滚动条左pos是0剩下的滚动是真实的
如果(scrollBarLeftPos == 0){
overScrollLeft = TRUE;
} 其他 {
overScrollLeft = FALSE;
}
//如果滚动条右侧POS优于网页宽度:对在滚动是真实的
如果(scrollBarRightPos> = innerWidth){
overScrollRight = TRUE;
} 其他 {
overScrollRight = FALSE;
}
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN://当用户触摸屏幕
//如果滚动条被左或右最
如果(overScrollLeft || overScrollRight){
isScrolling = FALSE;
} 其他 {
isScrolling = TRUE;
}
oldX = event.getX();
打破;
案例MotionEvent.ACTION_UP://当用户停止触摸屏
//如果滚动条不能去更多地向左或向右
//这使强迫用户做另一种姿态,当他到达侧
如果(!isScrolling){
如果(event.getX()> oldX&安培;&安培; overScrollLeft){
//左行动
}
如果(event.getX()&其中; oldX&安培;&安培; overScrollRight){
//权之诉
}
}
打破;
默认:
打破;
}
返回super.onTouchEvent(事件);
}
}
I need to know how to detect if the user can't scroll anymore in a webView. I want to generate an action when the user swipes to the left or right, but only if the user can't scroll:
IF the user swipes to the left AND the webview can't scroll to left THEN
do something
ELSE
let the webview scroll
解决方案
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);
}
}
这篇关于机器人的WebView:检测滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!