Java Android WebView 滚动到底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24313263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:27:59  来源:igfitidea点击:

Android WebView scroll to bottom

javaandroidwebviewscroll

提问by Phil

I try to scroll down the page I have in my WebView. Therefore I found the function .pageDown(true). The problem is that this function does not really work for me. Mostly it does not do anything.

我尝试向下滚动 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);
                     }
                 });

Is there another method or is it wrong to use it in onPageFinished?

是否有另一种方法或者在onPageFinished中使用它是错误的?

回答by petey

get the html content height and use scrollTo(x,y)

获取 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());
                 }
             });

回答by Mr. Fish

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.

我有一个使用 scrollTo 函数向下滚动大文件(多行)的解决方案。我设置了一个比内容高度高得多的值。

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);
  } 
});

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.

现在的问题是,它不适用于小文件(较少的行),因为 onPageFinished 方法是在 WebView 呈现之前调用的,所以我必须使用延迟句柄。

回答by Vladimir

      wvChat.setWebViewClient(new WebViewClient() {
                     @Override
                     public void onPageCommitVisible(WebView view, String url) {
                        wvChat.pageDown(true);
                     }
                 });

this better, without delay...

这更好,毫不拖延......