如何知道webView是否在android中开始渲染

时间:2016-10-27 05:26:56

标签: android pdf webview android-webview

我正在WebView加载pdf。

WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient());

String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");

webView.loadUrl(url);
setContentView(webView);

每当活动开始时,WebView显示为空白。但我想显示一个进度对话框,直到WebView开始渲染pdf文件。如何向WebView添加一个监听器,表明文件已经开始对用户可见。

3 个答案:

答案 0 :(得分:1)

公共类FGWebViewClient扩展了WebViewClient {

private WebViewClient wvc;
private static final String TAG=FGWebViewClient.class.getCanonicalName();
ProgressDialog mProgressDialog;

 // when you add the webviewclient then show the progressDialog.
public FGWebViewClient(Context context) {
    Log.i(TAG, "FGWebViewClient Called...");

   this.wvc=wvc;
    mProgressDialog=new ProgressDialog(context);
    mProgressDialog.setMessage("URL Loading...");
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mProgressDialog.setCanceledOnTouchOutside(false);


    if(mProgressDialog!=null){
        mProgressDialog.show();
    }



}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG,"shouldOverrideUrlLoading");
    return  wvc.shouldOverrideUrlLoading(view, url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

    Log.i(TAG,"onPageStarted");

    wvc.onPageStarted(view, url, favicon);
}
 // OnpageFinished just dismiss the progressDialog.
@Override
public void onPageFinished(WebView view, String url) {

    if(mProgressDialog!=null&&mProgressDialog.isShowing()){
         mProgressDialog.dismiss();
         mProgressDialog=null;
     }

    Log.i(TAG,"onPageFinished");
    //To clear cache of webview
    //view.clearCache(true);
    //clearEveryThing(view);

    wvc.onPageFinished(view,url);
}

并像这样打电话

WebView webView=(WebView)v.findViewById(R.id.webView1);

        FGWebViewClient webViewClient=  new FGWebViewClient(context);
        webView.setWebViewClient(webViewClient);


        webView.setWebChromeClient(new WebChromeClient());
        webView.clearCache(true);
        webView.clearHistory();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.loadUrl((webUrl).toString());   

答案 1 :(得分:1)

您可以使用setWebViewClient并将showProgress显示为以下代码

WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setPluginState(WebSettings.PluginState.ON);
            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

            showProgress();
            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                public void onPageFinished(WebView view, String url) {
                    hideProgress();
                }

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    hideProgress();
                }

            });

String url = "http://drive.google.com/viewerng/viewer?embedded=true&url=http://" + getIntent().getStringExtra("url");

webView.loadUrl(url);

答案 2 :(得分:0)

您可以创建自定义类并使用WebViewClient对其进行扩展。然后覆盖方法onPageStarted,您可以考虑它开始渲染。例如:

private class CustomWebClient extends WebViewClient {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
      super.onPageStarted(view, url, favicon);

      showProgress();

   }
   @Override
   public void onPageFinished(WebView view, String url) {
       super.onPageFinished(view, url);
       dismissDialog();
   }

}