由于某种原因,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()不起作用.它在网页浏览实际呈现页面之前发射得太快了.
相反,我不得不使用一个“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. } }