Android webview:检测滚动
我需要知道如何检测用户是否不能再在webView中滚动。我想在用户向左或向右滑动时生成一个动作,但前提是用户无法滚动:Android webview:检测滚动,android,webview,scroll,Android,Webview,Scroll,我需要知道如何检测用户是否不能再在webView中滚动。我想在用户向左或向右滑动时生成一个动作,但前提是用户无法滚动: IF the user swipes to the left AND the webview can't scroll to left THEN do something ELSE let the webview scroll API中有一些有趣的方法,但它们是受保护的,我认为您可以创建自己的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
API中有一些有趣的方法,但它们是受保护的,我认为您可以创建自己的WebView(扩展WebView)并使用这些方法并将其公开。 具体来说,也许你可以用这两个
而且。我找到了一个适合我的解决方案,我检查了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);
}
}
公共类CustomWebView扩展了WebView{
私有浮动oldX;
//指示水平滚动条是否不能向左移动
私有布尔overScrollLeft=false;
//指示水平滚动条是否不能向右移动
私有布尔overScrollRight=false;
//指示水平滚动条是否不能向左或向右移动
私有布尔isScrolling=false;
公共CustomWebView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
公共CustomWebView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
//TODO自动生成的构造函数存根
}
公共CustomWebView(上下文、属性集属性){
超级(上下文,attrs);
//TODO自动生成的构造函数存根
}
@凌驾
公共布尔onTouchEvent(运动事件){
//垂直滚动条的宽度
int scrollBarWidth=getVerticalScrollbarWidth();
//视图的宽度取决于您在布局中设置的宽度
int viewWidth=computeHorizontalScrollExtent();
//网页的宽度取决于缩放
int innerWidth=computeHorizontalScrollRange();
//水平滚动条左侧的位置
int scrollBarLeftPos=computeHorizontalScrollOffset();
//水平滚动条右侧的位置,滚动条的宽度是视图宽度减去垂直滚动条的宽度
int scrollBarRightPos=scrollBarLeftPos+viewWidth-scrollBarWidth;
//如果滚动条的左位置为0,则剩余滚动为真
如果(scrollBarLeftPos==0){
overScrollLeft=真;
}否则{
过交叉左=假;
}
//如果滚动条的右位置优于网页宽度:右上方滚动为真
如果(scrollBarRightPos>=内部宽度){
overScrollRight=真;
}否则{
overScrollRight=错误;
}
开关(event.getAction()){
case MotionEvent.ACTION_DOWN://当用户触摸屏幕时
//如果滚动条位于最左侧或最右侧
如果(过交叉左| |过交叉右){
IsCrolling=假;
}否则{
isScrolling=true;
}
oldX=event.getX();
打破
case MotionEvent.ACTION_UP://当用户停止触摸屏幕时
//如果滚动条不能向左或向右移动
//这允许用户在到达一侧时强制做另一个手势
如果(!IsCrolling){
if(event.getX()>oldX&&overScrollLeft){
//左作用
}
if(event.getX()
我认为这将解决这个问题:
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);
onOverScroll方法适用于android 2.3,因此我将获得webview 2.3的源代码,并将其包含在我的项目中。当内容放大或缩小时,这不起作用。有解决办法吗?