Android Webview.scrollto Is Not Working
I'm calling to webview.scrollTo in onPageFinished function, but it doesn't do anything. public void onPageFinished(WebView view, String url) { // TODO Auto-generat
Solution 1:
It appears to me that there's a race condition between the completion of onPageFinished
, onProgressChanged
, WebView.scrollTo
, and the display (actually drawing to the screen) of the web page.
After the page is displayed, the WebView
'thinks' it has scrolled to your scrollY
position.
To test, you could verify that WebView.getScrollY()
returns what you desire, but the display of the page is not in that position.
To work around this issue, here is a non-deterministic approach to scroll to Y immediately after the page is presented:
...
webView = (WebView) view.findViewById(R.id.web_view_id);
webView.loadData( htmlToDisplay, "text/html; charset=UTF-8", null);
...
webView.setWebViewClient( newWebViewClient()
{ ... } );
webView.setWebChromeClient(newWebChromeClient()
{
...
@OverridepublicvoidonProgressChanged(WebView view, int progress) {
...
if ( view.getProgress()==100) {
// I save Y w/in Bundle so orientation changes [in addition to// initial loads] will reposition to last location
jumpToY( savedYLocation );
}
}
} );
...
privatevoidjumpToY( int yLocation ) {
webView.postDelayed( newRunnable () {
@Overridepublicvoidrun() {
webView.scrollTo(0, yLocation);
}
}, 300);
}
The final parameter of 300 ms appears to allow the system to 'catchup' before the jumpToY is invoked. You might, depending upon platforms this runs on, play with that value.
Hope this helps
-Mike
Post a Comment for "Android Webview.scrollto Is Not Working"