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.
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.
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.
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.
Can you try something like this:
if (_webView.getScrollY() + _webView.getHeight() > _webView.getContentHeight())
_webView.scrollTo(0, _webView.getContentHeight() - _webView.getHeight());
There are some threads running on how to make scrolling and page turns e-ink friendly:
http://github.com/aarddict/android/issues/28#issuecomment-3512595 http://www.mobileread.com/forums/showthread.php?p=1929476#post1929476
来源:https://stackoverflow.com/questions/8561866/prevent-unwanted-flicker-due-to-innate-webview-behavior