Webview加载对话框

时间:2011-09-26 13:10:03

标签: java android

下面我的代码在初始加载时将进度对话框加载到webview中,但我希望每次webview加载URL时都会显示它!

我认为我需要在进度条之前使用onpagestarted()但是我有点像这样的菜鸟并且无法使其正常工作。有人可以提出建议吗?

    //need to set progress bar condition here!

    progressBar = ProgressDialog.show(MyActivity.this, "Loading title", "Loading...");

    myWebView.setWebViewClient(new WebViewClient() {

         public void onPageFinished(WebView view, String url) {
             Log.i(TAG, "Finished loading URL: " +url);
             if (progressBar.isShowing()) {
                 progressBar.dismiss();
             }
         }

    });
    //web site load
    myWebView.loadUrl("http://www.google.com");

2 个答案:

答案 0 :(得分:2)

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  
    }

   @Override
   public void onPageFinished(WebView view, String url) {
       if(!redirect){
          loadingFinished = true;
       }

       if(loadingFinished && !redirect){
         //HIDE LOADING IT HAS FINISHED
       } else{
          redirect = false; 
       }

    }
});

如下所示:How to listen for a WebView finishing loading a URL?

答案 1 :(得分:0)

您应该使用onPageStarted()方法来显示ProgressDialog

myWebView.setWebViewClient(new WebViewClient() {

     public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return false;
     }

     public void onPageStarted(WebView view, String url, Bitmap favicon) {
          progressBar.show();
     }

     public void onPageFinished(WebView view, String url) {
          progressBar.hide();
     }

});