简体   繁体   中英

How to scroll WebView to top in android?

I am working on a webView in an app, where content which webview load changes when a button is pressed (two buttons next and previous , they just change the content in webview).

If I scroll down to a point in webview and press next or previous button it starts from the same point. I just want that everytime the new data is loaded to webview it display the content from the top. I have used:

webView.scrollTo(0, 0);

But doesnt work.How can it work ?Please help anyone?

You can use a webviewclient and set the onPageFinished to scroll to the top: How to listen for a WebView finishing loading a URL?

mWebView.setWebViewClient(new WebViewClient() {
  public void onPageFinished(WebView view, String url) {
    view.scrollTo(0,0);
  }
});

That way, the content will be loaded when you scroll. It may feel a bit sluggish, but it should work every time.

Maybe delaying it would work? as the content doesn't usualy load instantly

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                webView.scrollTo(0,0);

            }
        }, 500);

使用线程并延迟scrollTo使它工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM