5

我有一个要求,我需要在其下方显示一个带有复选框的 WebView。该复选框最初处于禁用状态。只有当用户向下滚动到 webview 的底部时,才会启用该复选框。我通过扩展 Webview 类并编写我的 onScrollChanged 侦听器来做到这一点,如下所示:

@Override
protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) {
     if ( (getContentHeight() - (top + getHeight())) <= mMinDistance )
     {
         Log.i("MYACT","content height: "+getContentHeight()+" top: "+top+" Height: "+getHeight()+" minDistance: "+mMinDistance);
         Log.i("MYACT", "Reached bottom");
         mOnBottomReachedListener.onBottomReached(this);
     }
     else{
         Log.i("MYACT","content height: "+getContentHeight()+" top: "+top+" Height: "+getHeight()+" minDistance: "+mMinDistance);
         Log.i("MYACT", "Not at bottom");
         mOnBottomReachedListener.onNotAtBottom(this);
     }
     super.onScrollChanged(left, top, oldLeft, oldTop);
}

问题是条件

(getContentHeight() - (top + getHeight())) <= mminDistance) //mminDistance 在我的例子中是 0

甚至在我向下滚动到页面底部之前都很满意。如您所见,我尝试在日志中打印每个值,发现参数"top"有时超过值"getContentHeight"

样本日志值:

08-14 11:28:19.401:I/MYACT(1075):内容高度:3020 顶部:3861 高度:416 分钟距离:0

在这种情况下,我怎样才能避免这种情况?我是否应该使用不同的方式来检查我是否已向下滚动到页面底部?

谢谢

4

2 回答 2

6

经过一番研究,我发现了这个问题。Webview 通常会根据屏幕大小对网页进行缩放,然后进行渲染。它通常会缩放页面,使其在手机中看起来不会太小。您可以使用getScale()方法获取此比例因子

因此,就我而言,我将使用以下条件来检查是否已到达页面末尾

(getContentHeight()*getScale() - (top + getHeight())) <= mminDistance) //mminDistance 在我的例子中是 0

于 2013-08-14T18:27:55.197 回答
0

If memory serves values returned by getContentHeight() may depend on implementation of a WebView and its rendering mode (software/hardware). But I'm not sure. Perhaps this talk at Google IO will help you more in this question.

But generally speaking it's always hard and tricky to mix WebView with other views, especially when it comes to scrolling.

So my advice would be to put your checkbox into HTML displayed by the WebView. You have pretty much options of doing that like injecting some additional HTML code before passing it to loadData() method or invoking JavaScript with loadUrl("javascript:whatever();"), which can modify the initial DOM structure.

As a rule I do not put other views to Activity window when I have to deal with a WebView there.

于 2013-08-14T16:51:22.170 回答