Android的ProgessBar而加载的WebView [英] Android ProgessBar while loading WebView
问题描述
在我的应用程序,我有一个的WebView它加载从互联网上的任何URL。现在,有时由于较慢的网络页面需要很长的时间来加载,用户只能看到空白屏幕。
In my application, I have a WebView which loads any URL from the internet. Now, sometimes due to slow networks the page takes a long time to load and the user sees only a blank screen.
我要显示一个进度条,而web视图被加载,隐藏在web视图被完全加载progessBar。
I want to show a progressBar while the webView gets loaded and hide the progessBar when the webView gets loaded completely.
我知道如何使用的进度和AsyncTasks,但这里是我的问题。
I know how to use the progressBar and AsyncTasks, but here is my problem.
这是我用来加载web视图我的code。
This is the code that I use to load my webView.
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl(web_URL);
和本我的自定义WebViewClient类
And this my custom WebViewClient class
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
现在,如果我尝试显示使用进度异步任务,然后我想我将不得不给code要加载的URL在 doInBackGround()我的AsyncTask的功能,并显示通过 onProgressUpdate()函数的进展。
Now, if I try to show the progressBar using Async Tasks then I guess I would have to give the code to load the URL in the doInBackGround() function of my AsyncTask and show the progress through the onProgressUpdate() function.
不过,我怎么加载的URL doInBackground()为 doInBackground()在在非UI线程上运行,我将无法使用<强> mWebView.loadUrl(的web_url)里面。
But, how do I load the URL inside the doInBackground() as doInBackground() runs on the Non-UI thread and I wont be able to use mWebView.loadUrl(web_URL) inside it.
有什么建议?我失去了一些东西明显?请指引我。
Any suggestions ? Am I missing something obvious ? Please guide me.
推荐答案
检查源$ C $ C。帮助您和您解决问题...
Check the source code. Help you and solve your problem...
public class AppWebViewClients extends WebViewClient {
private ProgressBar progressBar;
public AppWebViewClients(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
我觉得它帮助你。
I think it help you.
感谢。
这篇关于Android的ProgessBar而加载的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!