渲染网页时onPageFinished不触发正确网页时、正确、onPageFinished

2023-09-04 05:20:26 作者:一身痞子味

由于某些原因,onPageFinished被解雇之前web视图已完成加载 - 我想不通为什么...

For some reason the onPageFinished is firing before the WebView has finished loading - I can't figure out why...

public class WebViewClientTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final WebView webview = (WebView) findViewById(R.id.webview);

    webview.setWebViewClient(new WebViewClient() {  
        @Override  
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(webview, url);
            webview.scrollTo(0, 500);
        }  
    });
    webview.loadUrl("http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=lala");

}
}

确定,以及它看起来像这样是不固定的。我觉得有一个竞争条件去加载页面的时候,但无法获得可重复的行为。

OK, well it looks like this isn't fixed. I think there's a race condition going on when loading the page, but can't get a reproducible behaviour.

我存储网页的SQLite数据库的HTML内容进行离线时查看。我重装内容,存入的WebView有:

I'm storing the HTML content of a webpage in a SQLite database for viewing when offline. I reload the content into the WebView with:

webView.loadDataWithBaseURL("fake://fake.com/", htmlBody, "text/html", "utf-8", null);

看来,有时当web视图加载它正确地触发WebViewClient.onPageFinished()方法,而其他时候没有。有时,这似乎触发前的页面加载完成后,产生0或contentHeight和忽略任何scrollTo要求。

It seems that sometimes when the WebView loads it fires the WebViewClient.onPageFinished() method correctly, and other times it does not. Sometimes it appears to fire before the page has finished loading, producing a contentHeight of 0 and ignoring any scrollTo calls.

任何人有任何这方面的经验吗?

Anyone have any experience with this?

推荐答案

我有一个项目,有code这需要运行后,才web视图已经显示它的内容,和你一样,onPageFinished()不加工。它被炒得很快,以前的WebView实际上呈现的页面。

I had a project that had code which needed to run only after the webview had displayed it's content, and like you, onPageFinished() wasn't working. It fired too quickly, before the webview had actually rendered the page.

相反,我不得不使用PictureListener,这会触发的WebView实际更新屏幕。

Instead, I had to use a "PictureListener" which gets fired when the webview actually updates the screen.

您使用它像这样:

mWebView.setPictureListener(new MyPictureListener());
//... and then later on....
class MyPictureListener implements PictureListener {

    @Override
    public void onNewPicture(WebView view, Picture arg1) {
      // put code here that needs to run when the page has finished loading and
      // a new "picture" is on the webview.      
    }    
}