是否有一个监听器,当web视图显示它的内容? [英] Is there a listener for when the WebView displays it's content?

查看:530
本文介绍了是否有一个监听器,当web视图显示它的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WebViewClient和/或WebChromeClient你可以得到一个侦听器时在页面加载的,然而,这有时被称为前的WebView中有任何内容,之前就已经显示什么。

Using WebViewClient and/or the WebChromeClient you can get a listener for when the page has loaded, however this is sometimes called before the WebView has any content in it, before it has displayed anything.

什么是对当web视图显示了它的内容确定有效的方法?

What would be a efficient method for determining when the WebView has displayed it's content?

编辑:(试图更清晰)

当我在web视图下载一个页面,我想滚动设置为特定的位置。似乎滚动位置不能设定,直至装入页,它有一个实际的内容的高度。所以,我已经尝试了两种不同的方法,当页面加载完成后,onPageFinished()从WebViewClient也onProgressChanged()从WebChromeClient确定。这两种告诉我,当网页加载完毕。

When I load a page in a WebView, I want to set the scroll to a specific position. It seems that the scroll position cannot be set until the page is loaded and it has an actual content height. So, I have tried two different approaches to determining when the page has finished loading, onPageFinished() from the WebViewClient and also onProgressChanged() from the WebChromeClient. Both of these tell me when the page has finished loading.

然而,问题是,有时它的页被显示之前,因此页没有高度和滚动呼叫不执行任何调用。

However, the problem is that sometimes it is called before the page has been displayed and therefore the page has no height and the scroll call does nothing.

我试图找到一种可靠的方法来确定网页时随时可以滚动,即。当它有它的内容的高度

I am trying to find a solid way to determine when the page is ready to be scrolled, ie. when it has its content height.

我想我可以在安装后它完成加载,以继续寻找时,高度可用,但似乎像相当破解一个检查循环。希望有一个更清洁的方式。

I imagine I could setup a checking loop after it finished loading to keep looking for when the height is available but that seemed like quite the hack. Hoping there is a cleaner way.

推荐答案

我成功地使用理查德的一个PictureListener 获得答案几年下来,但我不再推荐这是最好的解决方案。

I successfully used Richard's answer with a PictureListener for a few years, but I no longer recommend this as the best solution.

这是出于两个原因:

  1. <一个href="http://developer.android.com/reference/android/webkit/WebView.html#setPictureListener%28android.webkit.WebView.PictureListener%29">webView.setPictureListener和<一href="http://developer.android.com/reference/android/webkit/WebView.PictureListener.html">PictureListener都德precated。
  2. 在使用这个监听器会导致的WebView经常分配一个新的图片。这种分配是昂贵的,这可能有一些显著的性能影响,甚至导致本机崩溃的软糖MR1。
  1. webView.setPictureListener and PictureListener are both deprecated.
  2. Using this listener will cause the WebView to allocate a new Picture often. This allocation is expensive and this can have some significant performance impacts or even cause native crashes on JellyBean MR1.

相反,我建议创建的WebView)的一个子类和压倒一切的无效(比如:

Instead I recommend creating a subclass of WebView and overriding invalidate() like so:

@Override
public void invalidate() {
    super.invalidate();

    if (getContentHeight() > 0) {
        // WebView has displayed some content and is scrollable.
    }
}

如果你仍然想使用PictureListener方法,你将得到更好的性能,如果你setPictureListener回空后,你用它做。

If you still want to use the PictureListener method, you will get better performance if you setPictureListener back to null after you are done with it.

这篇关于是否有一个监听器,当web视图显示它的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆