Android WebView滚动到底部

Phi*_*hil 0 java android scroll webview

我尝试向下滚动我在WebView中的页面.因此我找到了这个功能.pageDown(true).问题是这个功能对我来说并不适用.大多数情况下它什么都不做.

Codesnippet:

wvChat.loadData(chat_, "text/html; charset=utf-8", "UTF-8");
                 wvChat.setWebViewClient(new WebViewClient() {
                     public void onPageFinished(WebView view, String url) {
                        wvChat.pageDown(true);
                     }
                 });
Run Code Online (Sandbox Code Playgroud)

是否有其他方法或在onPageFinished中使用它是错误的?

pet*_*tey 6

获取html内容高度并使用scrollTo(x,y)

wvChat.loadData(chat_, "text/html; charset=utf-8", "UTF-8");
             wvChat.setWebViewClient(new WebViewClient() {
                 @Override
                 public void onPageFinished(WebView view, String url) {
                    //use the param "view", and call getContentHeight in scrollTo
                    view.scrollTo(0, view.getContentHeight());
                 }
             });
Run Code Online (Sandbox Code Playgroud)


Mr.*_*ish 5

I have a solution to scroll down large files (much lines) using the scrollTo function. I set a much higher value than the content height can be.

mWebView.setWebViewClient(new WebViewClient()
{
  @Override
  public void onPageFinished(WebView view, String url)
  {
    super.onPageFinished(view, url);     

    Handler lHandler = new Handler();
    lHandler.postDelayed(new Runnable()
    {             
      @Override
      public void run()
      {
        mWebView.scrollTo(0, 1000000000);
      }
    }, 200);
  } 
});
Run Code Online (Sandbox Code Playgroud)

The problem is now, that it does not work for small files (less lines), because the onPageFinished method is called before the WebView has been rendered, so I have to use a delay handle.