如何显示进度条,直到WebView内容出现在android中

时间:2014-11-18 07:38:19

标签: android webview progress

我正在尝试使用Progress Bar直到加载WebView的内容。

in xml

            <ProgressBar android:id="@+id/progressbar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:max="100"
                android:visibility="gone" />

            <WebView
                android:id="@+id/intro2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

在java中

    ProgressBar pbr= (ProgressBar)findViewById(R.id.progressbar);
    pbr.setProgress(0);
    pbr.setVisibility(View.VISIBLE);

    WebView view = (WebView) findViewById(R.id.mgs1);
    String text = "<html><head><style type=\"text/css\">table, th, td { table-layout: fixed; padding: 2px;  text-align: center; background-color: #339933; border: 2px solid white; border-collapse: collapse; }\"></style></head> <body style=\"text-align:justify; color:#ffffff; margin-right :30px; background-color:#339933;\"> %s </body></Html>";
    String data = "<table style=width: 30%; > <tr><td>17</td> <td>24</td> <td>1</td> <td>8</td> <td>15</td></tr>  </table>";
    view.loadData(String.format(text, data), "text/html", "utf-8");
    view.getSettings().setDefaultFontSize(12);

在此之后该怎么办?

3 个答案:

答案 0 :(得分:2)

出于您的目的,将setWebViewClient用于webview,如下所示。阅读t开始和停止进度条的注释。

view.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            // Show progressbar
            pbr.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) {
            // Show error 
            // Stop spinner or progressbar
            pbr.setVisibility(View.GONE);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // Stop spinner or progressBar
            pbr.setVisibility(View.GONE);

        }
    });

答案 1 :(得分:0)

在onCreate

中添加以下2行
    String url = "your url";
    new LoadSocialNetworkUrlTask().execute(url);        

以下是asynctask

    public class LoadSocialNetworkUrlTask extends AsyncTask<String, String, Void> {

    protected void onPreExecute() {
        dialog = new ProgressDialog(SocialNetworkActivity.this);
        dialog.setMessage(getResources().getString(R.string.loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();
    }

    protected void onProgressUpdate(final String... url) {
        try {

            ((WebView) webView).getSettings().setJavaScriptEnabled(true);
            ((WebView) webView).setBackgroundColor(Color.TRANSPARENT);

            ((WebView) webView).setWebViewClient(new WebViewClient() {

                @Override
                public void onReceivedSslError(WebView view,
                        SslErrorHandler handler, SslError error) {
                    handler.proceed(); // Ignore SSL certificate errors
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO hide your progress image
                    super.onPageFinished(view, url);

                    dialog.dismiss();
                }
            });
            ((WebView) webView).loadUrl(url[0]);

        } catch (Exception e) {
            e.printStackTrace();
            dialog.dismiss();
        }

    }

    @Override
    protected Void doInBackground(String... url) {
        try {
            publishProgress(url);

        } catch (Exception e) {
            e.printStackTrace();
            dialog.dismiss();
        }

        return null;
    }
}

答案 2 :(得分:0)

你可以这样做

 view.setWebChromeClient(new WebChromeClient() {
                   public void onProgressChanged(WebView view, int progress) {
                     // Activities and WebViews measure progress with different scales.
                     // The progress meter will automatically disappear when we reach 100%
                     pbr.setVisibility(ProgressBar.VISIBLE);
                    pbr.setProgress(progress);


                    if(progress==100){
                        pg.setVisibility(ProgressBar.GONE);
                    }

                   }
                 });