由于先天的WebView行为prevent不必要的闪烁先天、不必要、行为、WebView

2023-09-04 13:20:49 作者:手心里的温柔

我使用的是Android的WebView显示一些HTML内容(字符串 HTML标记为precise)在运行时产生的。内容有基本的HTML <表...> 显示各种东西和我的程序生成此表中的行。 HTML内容在我的WebView通过调用 loadDataWithBaseUrl()方法加载。 现在,每次我生成一个新行,我创建了一个新的< TR>< / TR> 和修改HTML字符串到新创建的行添加到表中。 很显然,我有权罢免本 loadDataWithBaseUrl()的方法来重新加载HTML和显示最新的行。

I am using an Android WebView to show some HTML content (a String with HTML tags to be precise) generated at runtime. The content has basically an HTML <table ...> showing various stuff and the rows of this table are generated by my program. The HTML content is loaded in my WebView by calling the loadDataWithBaseUrl() method. Now everytime I generate a new row, I create a new <tr></tr> and modify the HTML string to add the newly created row to the table. Obviously I have to recall the loadDataWithBaseUrl() method to reload the HTML and show the latest row.

现在有一些问题在这里: 一)每次 loadDataWithBaseUrl()被调用时,web视图滚动到页面顶部 二)我要滚动的WebView 这是所有的方式加载到页面的底部之后,以使最新生成的行始终显示

Now there are a few problems here: First) Every time loadDataWithBaseUrl() is called, the WebView scrolls to the top of the page Second) I want to scroll the WebView after it is loaded all the way to the bottom of the page so that the latest generated row is always shown.

现在实际的问题: 如果我尝试调用的WebView 下页(真)方法后,页面加载完成,我会得到一个讨厌的动画,并且用户不得不等待web视图光洁度滚动到页面的底部。 因为似乎没有办法任何禁用此上下跳跃的行为,我使出了JavaScript和我所有的方式滚动页面的底部在页面加载后(调用函数窗口.onload ) 现在我面临的一个问题!不过有一个由web视图引起重装导致滚动所有的方式以及JavaScript函数立即滚动页面底部的页面闪烁。

Now the actual problem: If I try to call WebView's pageDown(true) method after the page has finished loading, I will get a nasty animation and the user has to wait for the WebView finish scrolling to the bottom of the page. As there seems to be no way whatsoever to disable this jumping up and down behavior, I resorted to JavaScript and I am scrolling the page all the way to the bottom after the page has loaded (calling a function on window.onload) Now I am facing another problem! Still there is a flicker caused by the WebView reloading the page which results in scrolling all the way up and the JavaScript function scrolling the page immediately to the bottom.

我花了无数的时间在过去的几天,还没有想出一个办法来达到我想要的效果。有没有办法来解决这个问题? (解决方法/不同的策略也许?)的罪魁祸首显然是滚动到顶部时,它完成加载页的web视图的先天行为。

I have spent countless hours in the past few days and still haven't figured out a way to achieve my desired results. Is there a way to solve this issue? (Workaround/different strategy maybe?) The culprit is obviously the innate behavior of the WebView that scrolls to the top when it finishes loading the page.

推荐答案

可以尝试这样的:

if (_webView.getScrollY() + _webView.getHeight() > _webView.getContentHeight())
    _webView.scrollTo(0, _webView.getContentHeight() - _webView.getHeight());