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

查看:2786
本文介绍了安卓webview.scrollTo不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话给webview.scrollTo在onPageFinished功能,但它不会做任何事情。

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?

推荐答案

在我看来,这有完成之间的竞争条件 onPageFinished onProgressChanged WebView.scrollTo ,显示(实际上在屏幕上绘图)的网页。

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.

显示页面后,的WebView '认为'这已经滚动到你的 scrollY 的位置。

After the page is displayed, the WebView 'thinks' it has scrolled to your scrollY position.

要测试,你可以验证 WebView.getScrollY()返回你的愿望,但页面的显示不在那个位置。

To test, you could verify that WebView.getScrollY() returns what you desire, but the display of the page is not in that position.

要解决此问题,这里是一个不确定性的方法到页面后立即滚动到Y为presented:

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

的300毫秒的最后一个参数似乎允许系统追赶的jumpToY被调用之前。你可能,这取决于该平台上运行,发挥与价值。

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.

希望这有助于

-Mike

这篇关于安卓webview.scrollTo不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆