进度条加载两次webview

时间:2014-05-22 10:18:31

标签: android webview progress

当我输入网址并按下go时,此进度条工作正常但当我访问webview中存在的任何网址时,它往往会加载两次。请提供有价值的建议。

public void onProgressChanged(WebView view, int newProgress) {      
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
progress.setVisibility(View.GONE);
} else{
progress.setVisibility(View.VISIBLE);}
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}

2 个答案:

答案 0 :(得分:1)

<强>的onCreate:

progressbar =(ProgressBar)findViewById(R.id.progressbar);

MainActivity上的WebViewClient方法

private final WebViewClient mWebClient = new WebViewClient()
    {@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            super.onPageStarted(view, url, favicon);
            proyect.this.url.setText(url);
            progressbar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            super.onPageFinished(view, url);
            progressbar.setVisibility(View.GONE);
        }

答案 1 :(得分:0)

我最近遇到了同样的问题。您可以在加载之前检查进度对话框是否为空。

private ProgressDialog dialog = null;
wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (dialog == null) {
                dialog = ProgressDialog.show(sectionActivity, "",
                        "Loading. Please wait...", true);
                dialog.setCancelable(true);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (dialog != null && dialog.isShowing()) {
                dialog.cancel();
            }
        }
    });