在android webview中添加自定义进度条

时间:2014-11-11 09:05:50

标签: android

我的android webview中心需要一个进度条,它必须在页面加载之前显示,并在页面加载时隐藏。怎么做到呢?任何人都可以帮助我吗?

这是我的webview代码:

 Bundle extras = getIntent().getExtras();
String title;
final String url;

if (!Datacon.checkInternetConnection(this)) {
    Toast.makeText(getApplicationContext(), "Check your Internet Connection!", Toast.LENGTH_LONG).show();
} else {

if (extras != null) {
    title = extras.getString("title");
    url = extras.getString("url");
    TextView text=(TextView) findViewById(R.id.textView1);
    text.setText(title);
    final WebView myWebView =(WebView)findViewById(R.id.WebView);
    myWebView.loadUrl(url);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
    loadingProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal); 

    myWebView.setWebViewClient(new WebViewClient(){


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

Button refresh = (Button) actionBar.getCustomView().findViewById(R.id.but2);
refresh.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        myWebView.loadUrl(url);

        }
    });

} 
}}

}

2 个答案:

答案 0 :(得分:1)

您需要在WebViewClient

中覆盖其他几种方法
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        loadingProgressBar.setVisibility(View.VISIBLE);
    }

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

我假设loadingProgressBar是一个类字段。

PS:作为旁注,我相信你覆盖shouldOverrideUrlLoading是不必要的。默认行为是在您指示它时加载URL。

答案 1 :(得分:0)

private void initializeProgressBar() {
if (progressBar == null){
progressBar = new ProgressDialog(this);
progressBar.setMessage(Constants.LOADING_MESSAGE);
progressBar.setCancelable(false);
}
}

Handler peogressBar = new Handler(){
public void handleMessage(android.os.Message msg) {
try{
switch (msg.what) {
case 1:
initializeProgressBar();
if(!progressBar.isShowing())
progressBar.show();
break;
case 2:
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
progressBar = null;

}
break;
}
}catch(Exception e){
e.printStackTrace();
}
};
};



wv_graphLink = (WebView) findViewById(R.id.wv_graphLink);
wv_graphLink.getSettings().setJavaScriptEnabled(true);
wv_graphLink.getSettings().setBuiltInZoomControls(true);
wv_graphLink.getSettings().setSupportZoom(true);
wv_graphLink.setInitialScale(1);
wv_graphLink.getSettings().setLoadWithOverviewMode(true);
wv_graphLink.getSettings().setUseWideViewPort(true);
// wv_graphLink.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
// wv_graphLink.setScrollbarFadingEnabled(false);
peogressBar.sendEmptyMessage(1);
wv_graphLink.setWebChromeClient(new WebChromeClient(){

public void onProgressChanged(WebView view, int progress) {

if(progress == 100){
peogressBar.sendEmptyMessage(2);


}

}
});

wv_graphLink.loadUrl("Your url");