如何在android中检测webview的scrollend?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20998108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect scrollend of webview in android?
提问by Rajat Sharma
Mine is a native app where I want to show/ hide a button if the user scrolls to the end of webview. I looked at an answer hereand got the entire idea of how to register the callback via interface. My major issue is that I am not able to get my calculations right inside the onScrollChanged method. I have tried combination of things like getHeight(), getContentHeight(), top etc but it just seems to fire too early. I tried with a simple page like google's with comparatively lesser content as well as a news page.
我的是一个本机应用程序,如果用户滚动到 webview 的末尾,我想在其中显示/隐藏一个按钮。我在这里查看了一个答案,并获得了如何通过接口注册回调的完整想法。我的主要问题是我无法在 onScrollChanged 方法中进行计算。我尝试过将 getHeight()、getContentHeight()、top 等组合起来,但它似乎触发得太早了。我尝试了一个简单的页面,比如谷歌的内容相对较少的页面以及一个新闻页面。
These logics work fine for a normal scroll view. Since the webpage has a lot of content, it could be messing up the calculations. Pasting a sample code: does not work.
这些逻辑适用于普通的滚动视图。由于网页有很多内容,它可能会扰乱计算。粘贴示例代码:不起作用。
@Override
protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) {
if ( mOnWebViewBottomReachedListener != null ) {
//if ( (getContentHeight() - (top + getHeight())) <= mMinDistance )
int diff = (getBottom() - (getHeight() + getScrollY()));
Log.e("values ", diff+" o");
if ((int) Math.floor(getContentHeight() * getScaleY()) == top)
mOnWebViewBottomReachedListener.onBottomReached(this);
}
super.onScrollChanged(left, top, oldLeft, oldTop);
}
Need help with this. Thanks.
需要这方面的帮助。谢谢。
回答by Swati Sachdeva
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
int height = (int) Math.floor(this.getContentHeight() * this.getScale());
int webViewHeight = this.getMeasuredHeight();
if(this.getScrollY() + webViewHeight >= height){
Log.i("THE END", "reached");
}
super.onScrollChanged(l, t, oldl, oldt);
}
This logic works fine for a webview.
此逻辑适用于 web 视图。
回答by Aurel Nica
After a lot of google search all the solutions I found were calculating the height and determining the scrollOfset, but most of them are not reliable in most cases resulting in issues like described here Weird miscalculation when trying to detect if WebView scrolled to bottom
经过大量谷歌搜索,我找到的所有解决方案都在计算高度并确定 scrollOfset,但在大多数情况下,它们中的大多数都不可靠,导致这里描述的问题尝试检测 WebView 是否滚动到底部时出现奇怪的错误计算
I have found the solution that works 100% is to determine it based on overScroll
我发现 100% 有效的解决方案是根据 overScroll 确定它
webView.setOnOverScrolledCallback(new WebViewCustom.OnOverScrolledCallback() {
@Override
public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
if(clampedY)
if(scrollY == 0) {
//top
}
else {
//bottom
}
}
});
回答by Cüneyt
Firstly extend your webview from this class. https://stackoverflow.com/a/14753235/3027225After this upgrade your webView like this
首先从这个类扩展你的 webview。https://stackoverflow.com/a/14753235/3027225在此之后像这样升级你的 webView
<com.YourPackageName.ObservableWebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
And finally override the onScroll method like the following:
最后重写 onScroll 方法,如下所示:
webView.setOnScrollChangedCallback(new OnScrollChangedCallback() {
@Override
public void onScroll(int l, int t) {
int tek = (int) Math.floor(webView.getContentHeight() * webView.getScale());
if(tek - webView.getScrollY() == webView.getHeight())
Toast.makeText(getActivity(), "End", Toast.LENGTH_SHORT).show();
}
});
回答by Abhishek
This three method combination work excellent for me
这三种方法组合对我来说效果很好
Create two boolean variable two detect topReached or bottomReached.
use computeVerticalScrollRange() to get vertical scroll range. computeVerticalScrollRange() will help you when content height is less(In such case onScrollChanged not called).
- use onScrollChanged to detect scrolling is happing. scrolling is happingmeans content height is greater than webView measured height.Now detect bottom end using newt or detect top using newt.
创建两个布尔变量,两个检测topReached 或bottomReached。
使用 computeVerticalScrollRange() 获取垂直滚动范围。当内容高度较小时,computeVerticalScrollRange() 将帮助您(在这种情况下不调用 onScrollChanged)。
- 使用 onScrollChanged 来检测滚动是否发生。 滚动正在发生意味着内容高度大于 webView 测量的高度。现在使用 newt 检测底端或使用 newt 检测顶部。
private boolean topReached = false, bottomReached = false;
@Override
protected int computeVerticalScrollRange() {
int readerViewHeight = getMeasuredHeight();
int verticalScrollRange = super.computeVerticalScrollRange();
if (readerViewHeight >= verticalScrollRange) {
topReached = true;
bottomReached = true;
}
return verticalScrollRange;
}
@Override
protected void onScrollChanged(int newLeft, int newTop, int oldLeft, int oldTop) {
Logger.i(TAG, "onScrollChanged");
topReached = false;
bottomReached = false;
Log.i(TAG, "onScrollChanged newLeft : " + newLeft);
Log.i(TAG, "onScrollChanged newTop : " + newTop);
Log.i(TAG, "onScrollChanged oldLeft : " + oldLeft);
Log.i(TAG, "onScrollChanged oldTop : " + oldTop);
int readerViewHeight = getMeasuredHeight();
int contentHeight = getContentHeight();
if (newTop == 0) {
Log.d(TAG, "onScrollChanged : Top End");
topReached = true;
} else if (newTop + readerViewHeight >= contentHeight) {
Log.d(TAG, "onScrollChanged : Bottom End");
bottomReached = true;
}
super.onScrollChanged(newLeft, newTop, oldLeft, oldTop);
}