由于某种原因,onPageFinished在WebView完成加载之前被触发 - 我无法弄清楚为什么......
public class WebViewClientTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final WebView webview = (WebView) findViewById(R.id.webview); webview.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(webview, url); webview.scrollTo(0, 500); } }); webview.loadUrl("http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=lala"); } }
好吧,看起来这不是固定的.我认为加载页面时会出现竞争情况,但无法获得可重现的行为.
我将网页的HTML内容存储在SQLite数据库中,以便在离线时查看.我将内容重新加载到WebView中:
webView.loadDataWithBaseURL("fake://fake.com/", htmlBody, "text/html", "utf-8", null);
似乎有时当WebView加载它时会正确地触发WebViewClient.onPageFinished()方法,有时则不会.有时它似乎在页面加载完成之前触发,产生contentHeight为0并忽略任何scrollTo调用.
有人对此有经验吗?
我有一个项目,只有在webview显示内容后才需要运行代码,就像你一样,onPageFinished()无效.在webview实际呈现页面之前,它的启动速度太快了.
相反,我不得不使用"PictureListener",当webview实际更新屏幕时会被触发.
你这样使用它:
mWebView.setPictureListener(new MyPictureListener()); //... and then later on.... class MyPictureListener implements PictureListener { @Override public void onNewPicture(WebView view, Picture arg1) { // put code here that needs to run when the page has finished loading and // a new "picture" is on the webview. } }