android webview.scrollTo不工作[英] android webview.scrollTo is not working

本文是小编为大家收集整理的关于android webview.scrollTo不工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在onpagefinished函数中呼叫webview.scrollto,但它没有做任何事情.

    public void onPageFinished(WebView view, String url) 
    {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        webview.scrollTo(0, scrollY);
    }

为什么? 如何在装入加载后自动滚动页面?

推荐答案

在我看来,在onPageFinished,onProgressChanged,WebView.scrollTo的完成之间存在竞争条件,以及网页的显示器(实际绘制到屏幕)之间.

显示页面后,WebView"认为"它已滚动到scrollY位置.

测试,您可以验证WebView.getScrollY()返回您想要的内容,但页面的显示不在该位置.

要解决此问题,这里是在页面呈现后立即滚动到y的非确定性方法:

    ...
    webView = (WebView) view.findViewById(R.id.web_view_id);
    webView.loadData( htmlToDisplay, "text/html; charset=UTF-8", null);
    ...
    webView.setWebViewClient( new WebViewClient() 
    { ... } );
    webView.setWebChromeClient(new WebChromeClient() 
    {
        ...

        @Override
        public void onProgressChanged(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 );
            }
        }

     } );

    ...

  private void jumpToY ( int yLocation ) {
      webView.postDelayed( new Runnable () {
          @Override
          public void run() {
              webView.scrollTo(0, yLocation);
          }
      }, 300);
  }

300 ms的最终参数似乎允许系统在调用jumptoy之前"捕获".您可能会根据平台运行,播放该值.

希望这有助于

-mike

本文地址:https://www.itbaoku.cn/post/102531.html

问题描述

I'm calling to webview.scrollTo in onPageFinished function, but it doesn't do anything.

    public void onPageFinished(WebView view, String url) 
    {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        webview.scrollTo(0, scrollY);
    }

Any idea why? How can I scroll a page automatically after it finished to load?

推荐答案

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( new WebViewClient() 
    { ... } );
    webView.setWebChromeClient(new WebChromeClient() 
    {
        ...

        @Override
        public void onProgressChanged(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 );
            }
        }

     } );

    ...

  private void jumpToY ( int yLocation ) {
      webView.postDelayed( new Runnable () {
          @Override
          public void run() {
              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