Android WebView + loadUrl with javascript + onPageFinished = lag

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5820237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:28:55  来源:igfitidea点击:

Android WebView + loadUrl with javascript + onPageFinished = lag

javascriptandroidwebview

提问by Sver

I have WebView which loads some page, when it's finished I apply some javascript magic to mess up with DOM. Everything is fine, page loads and onPageFinishedI just call wv.loadUrl(javascript);

我有加载一些页面的 WebView,当它完成时,我应用了一些 javascript 魔法来弄乱 DOM。一切都很好,页面加载,onPageFinished我只是打电话wv.loadUrl(javascript);

But I don't want to see loading process, and how javascript is working, I just need result, so I made my view invisible with wv.setVisibility(View.INVISIBLE);from the start, and make it visible again when everything is done. This is where problem occurs.

但我不想看到加载过程,以及 javascript 是如何工作的,我只需要结果,所以我wv.setVisibility(View.INVISIBLE);从一开始就让我的视图不可见,并在一切完成后让它再次可见。这就是问题发生的地方。

This piece of code should make view visible after javascript is finished, but wv.setVisibility(View.VISIBLE)fires before javascript. So for a moment I see page and how it is being changed by javascript. This is just ugly.

这段代码应该在 javascript 完成后使视图可见,但wv.setVisibility(View.VISIBLE)在 javascript 之前触发。所以有一段时间我看到了页面以及它是如何被 javascript 改变的。这太丑了。

  public void onPageFinished (WebView view, String url) {
         wv.loadUrl(javascript);
         view.getSettings().setLoadsImagesAutomatically(true);
         wv.setVisibility(View.VISIBLE);

    }

I got that loadUrl works asynchronously, so I tried to make another WebViewClient with just "make first view visible" method inside onPageFinished, and use it to call JS. But it just keeps crashing with NPE error.

我知道 loadUrl 是异步工作的,所以我尝试在里面创建另一个只有“使第一个视图可见”方法的 WebViewClient onPageFinished,并用它来调用 JS。但它只是不断因 NPE 错误而崩溃。

wv2.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished (WebView view, String url) {
     wv.setVisibility(View.VISIBLE);
}
});

For now I just added delay after javascript (SystemClock.sleep(5000)), but this is like.. yeah.

现在我只是在 javascript ( SystemClock.sleep(5000))之后添加了延迟,但这就像......是的。

采纳答案by Sver

Ok, I think I figured it out. We can have callback from the end of javascript that will fire "show view" - like hereor here(with handler, because I wanted to update ui). But it wont change anything, the problem seems to be in rendering speed. It's just slow. So for now I'll just stick with 1 second delay after firing JS.

好吧,我想我想通了。我们可以从 javascript 的末尾回调将触发“显示视图” - 就像这里这里(使用处理程序,因为我想更新 ui)。但它不会改变任何东西,问题似乎出在渲染速度上。只是很慢。所以现在我只会在触发 JS 后坚持 1 秒延迟。

回答by NotACleverMan

I don't think you're going to get an easy solution to this. All that Android can detect (like most browsers) is when the page is fully loaded, which means, all of it is downloaded. Javascript waits for it to be downloaded also, and THEN starts running. It's just the way it works. The only solution I can think of might just be in JQuery. I don't know much about it but there's usually solutions to things like this in it.

我不认为你会得到一个简单的解决方案。Android 可以检测的所有内容(与大多数浏览器一样)是页面完全加载的时间,这意味着所有页面都已下载。Javascript 也等待它被下载,然后开始运行。这就是它的工作方式。我能想到的唯一解决方案可能只是在 JQuery 中。我对此了解不多,但通常有解决此类问题的方法。