在WebView中加载大部分页面后关闭ProgressCircle

时间:2017-03-11 18:43:39

标签: android webview

所以我正在加载WebView并使用progressCircle来跟踪它。但是,在加载WebView中的所有图像之后,进度循环才会很好地关闭。我希望它在页面加载后立即关闭,无论加载了多少图像。截至目前,我的代码是:

webView.setWebChromeClient(new WebChromeClient() {
            private ProgressDialog progressCircle;

            @Override
            public void onProgressChanged(WebView view, int progress) {
                if (progressCircle == null) {
                    progressCircle = new ProgressDialog(view.getContext(), R.style.WebViewLoadingCircle);
                    progressCircle.setCancelable(true);
                    progressCircle.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
                    try {
                        progressCircle.show();
                    } catch (Exception e) {
                        Log.e(TAG, "Exception while showing progress circle", e);
                    }
                }

                if (progress == 100) {
                    progressCircle.dismiss();
                    progressCircle = null;
                }
            }
        });

如果网页加载后如何解除progressCircle? (我已经尝试了很多东西,包括玩进度变量,但没有一个有帮助)。谢谢!

1 个答案:

答案 0 :(得分:2)

webView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do your stuff here
                if (progressCircle != null) {
                    progressCircle.dismiss();
                    progressCircle = null;
                 }
     }
});

这个完整的代码任务:

    boolean loadingFinished = true;
    boolean redirect = false;

    mWebView.setWebViewClient(new WebViewClient() {

       @Override
       public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
           if (!loadingFinished) {
              redirect = true;
           }

       loadingFinished = false;
       webView.loadUrl(urlNewString);
       return true;
       }

       @Override
       public void onPageStarted(WebView view, String url) {
            loadingFinished = false;
            //SHOW LOADING IF IT ISNT ALREADY VISIBLE
if (progressCircle == null) {
                    progressCircle = new ProgressDialog(view.getContext(), R.style.WebViewLoadingCircle);
                    progressCircle.setCancelable(true);
                    progressCircle.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
                    try {
                        progressCircle.show();
                    } catch (Exception e) {
                        Log.e(TAG, "Exception while showing progress circle", e);
                    }
                }  
        }

       @Override
       public void onPageFinished(WebView view, String url) {

                      if (progressCircle != null) {
                        progressCircle.dismiss();
                        progressCircle = null;
                     }

        }
    });

享受它!