WebView是否显示内容时有监听器吗? [英] Is there a listener for when the WebView displays it's content?

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

问题描述

使用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.

什么是确定WebView何时显示内容的有效方法?

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

(尝试更清晰)

当我在WebView中加载页面时,我想将滚动条设置到特定的位置。似乎滚动位置无法设置,直到页面被加载并且具有实际的内容高度。所以,我已经尝试了两种不同的方法来确定页面何时完成加载,来自WebViewClient的onPageFinished()和来自WebChromeClient的onProgressChanged()。这两个都告诉我页面加载完成。

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.

推荐答案

我成功使用了 Richard的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. PictureListener 都被弃用。

  2. 使用此侦听器将导致WebView经常分配新的图片。这种分配是昂贵的,这可能会对JellyBean 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并覆盖invalidate(),如下所示:

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方法,如果将PictureListener设置为null,将会获得更好的性能。

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.

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

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