Auto-Scroll the Webview

☆樱花仙子☆ 提交于 2019-12-12 16:34:19

问题


I want my webview to autoscroll. Below is what I have tried, it does scroll the webview but it never stops i.e. it continues even after the webview has no content to display so it just displays the white screen. Please tell me how can it be fixed.

webview.setPictureListener(new PictureListener() {

                public void onNewPicture(WebView view, Picture picture) {
                    webview.scrollBy(0, 1);

                }
            });

回答1:


Try my code and hope it helps :) scroll down:

            mWebView.post(new Runnable() {
            public void run() {
                if (mWebView.getContentHeight() * mWebView.getScale() >= mWebView.getScrollY() ){
                    mWebView.scrollBy(0, (int)mWebView.getHeight());
                }
            }
        });

Scroll Up

mWebView.post(new Runnable() {
            public void run() {
                if (mWebView.getScrollY() - mWebView.getHeight() > 0){
                    mWebView.scrollBy(0, -(int)mWebView.getHeight());
                }else{
                    mWebView.scrollTo(0, 0);
                }
            }
        });



回答2:


This code work for auto scroll as well as your scroll goes down then automatically come to top and then again start scroll. Check and if any query update me.

Timer repeatTask = new Timer();
       repeatTask.scheduleAtFixedRate(new TimerTask() {
             @Override
             public void run() {
                   runOnUiThread(new Runnable() {
                    @Override
                     public void run() {
                          int height = (int) Math.floor(webView.getContentHeight() * webView.getScale());
                          int webViewHeight = webView.getMeasuredHeight();
                               if (webView.getScrollY() + webViewHeight >= height) {
                                      webView.scrollBy(0, 0);
                                      webView.scrollTo(0, 0);
                                  } else {
                                       webView.scrollBy(webView.getTop(), webView.getBottom());
                                  }
                               }
                 });
          }
}, 0, 5000);//delayed auto scroll time



回答3:


As best as I can tell, the only way to do this consistently with modern Android is with JavaScript:

    webView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            webView.evaluateJavascript("""
                |var _fullScrollIntervalID = setInterval(function() {
                |    if (window.scrollY + window.innerHeight >= document.body.scrollHeight) {
                |        window.clearInterval(_fullScrollIntervalID);
                |    } else {
                |        window.scrollBy(0, 10);
                |    }
                |}, 17);
            """.trimMargin(), null)
        }
    }

The JavaScript APIs are aware of the size of the content.

This solution doesn't take into account changing viewport sizes, window.innerHeight rounding errors, if document.body isn't the scrolling element, etc.


As for a Java-based solution, it seems the Java APIs give the size of the view, rather than the length of the page:

  • height
  • contentHeight
  • measuredHeight
  • bottom

Maybe this changed when enableSlowWholeDocumentDraw was introduced.

One Java API that is aware of the content length of the page is canScrollVertically but it returns false for a short time after onPageFinished is called. You could use some kind of delay to get around this. For example:

    webView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            ScrollRunnable().run()
        }
    }

// ...

val h = Handler()
inner class ScrollRunnable() : Runnable {
    override fun run() {
        // introduce some delay here!
        if (webView.canScrollVertically(1)) {
            webView.scrollBy(0, 10)
            h.postDelayed(this, 17L)
        }
    }
}

I tested this on an Android API26 emulator and an Android TV API 22 emulator.



来源:https://stackoverflow.com/questions/18168749/auto-scroll-the-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!