我的本机应用程序,如果用户滚动到webview的末尾,我想显示/隐藏按钮.我在这里查看了一个答案,并了解了如何通过接口注册回调.我的主要问题是我无法在onScrollChanged方法中进行计算.我尝试过getHeight(),getContentHeight(),top等组合,但它似乎太早了.我尝试使用像谷歌这样的简单页面,内容相对较少,也有新闻页面.
这些逻辑适用于普通滚动视图.由于网页内容很多,可能会搞乱计算.粘贴示例代码:不起作用.
@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); }
需要帮助.谢谢.
经过大量的google搜索后,我发现的所有解决方案都是计算高度并确定scrollOfset,但是大多数解决方案在大多数情况下都不可靠,导致出现此处所述的问题尝试检测WebView是否滚动到底部时出现奇怪的计算错误
我发现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 } } });
@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); }
这个逻辑适用于webview.