Android WebView 进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2537454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android WebView progress bar
提问by Max Marroni
回答by Yahyaotaif
For a horizontal progress bar, you first need to define your progress bar and link it with your XML file like this, in the onCreate
:
对于水平进度条,您首先需要定义您的进度条并将其与您的 XML 文件链接,如下所示onCreate
:
final TextView txtview = (TextView)findViewById(R.id.tV1);
final ProgressBar pbar = (ProgressBar) findViewById(R.id.pB1);
Then, you may use onProgressChanged Method in your WebChromeClient:
然后,您可以在 WebChromeClient 中使用 onProgressChanged 方法:
MyView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress < 100 && pbar.getVisibility() == ProgressBar.GONE){
pbar.setVisibility(ProgressBar.VISIBLE);
txtview.setVisibility(View.VISIBLE);
}
pbar.setProgress(progress);
if(progress == 100) {
pbar.setVisibility(ProgressBar.GONE);
txtview.setVisibility(View.GONE);
}
}
});
After that, in your layout you have something like this
在那之后,在你的布局中你有这样的东西
<TextView android:text="Loading, . . ."
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/tV1" android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#000000"></TextView>
<ProgressBar android:id="@+id/pB1"
style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_centerVertical="true"
android:padding="2dip">
</ProgressBar>
This is how I did it in my app.
这就是我在我的应用程序中所做的。
回答by Max Marroni
I have just found a really good example of how to do this here: http://developer.android.com/reference/android/webkit/WebView.html. You just need to change the setprogress from:
我刚刚在这里找到了一个很好的例子来说明如何做到这一点:http: //developer.android.com/reference/android/webkit/WebView.html。您只需要将 setprogress 更改为:
activity.setProgress(progress * 1000);
to
到
activity.setProgress(progress * 100);
回答by Md. Sajedul Karim
here is the easiest way to add progress bar in android Web View.
这是在 android Web 视图中添加进度条的最简单方法。
Add a boolean field in your activity/fragment
在您的活动/片段中添加一个布尔字段
private boolean isRedirected;
This boolean will prevent redirection of web pages cause of dead links.Now you can just pass your WebView object and web Url into this method.
这个布尔值将防止网页重定向导致死链接。现在你可以将你的 WebView 对象和 web Url 传递到这个方法中。
private void startWebView(WebView webView,String url) {
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
isRedirected = true;
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
isRedirected = false;
}
public void onLoadResource (WebView view, String url) {
if (!isRedirected) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(SponceredDetailsActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
}
public void onPageFinished(WebView view, String url) {
try{
isRedirected=true;
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
Here when start loading it will call onPageStarted
. Here i setting Boolean field is false. But when page load finish it will come to onPageFinished
method and here Boolean field is set to true. Sometimes if url is dead it will redirected and it will come to onLoadResource()
before onPageFinished
method. For this reason it will not hiding the progress bar. To prevent this i am checking if (!isRedirected)
in onLoadResource()
在这里开始加载时,它会调用onPageStarted
. 这里我设置布尔字段为假。但是当页面加载完成时,它会进入onPageFinished
方法,这里布尔字段设置为 true。有时,如果 url 已死,它将被重定向并且它会到达 onLoadResource()
before onPageFinished
方法。因此,它不会隐藏进度条。为了防止这种情况我检查if (!isRedirected)
中onLoadResource()
in onPageFinished()
method before dismissing the Progress Dialog you can write your 10 second time delay code
在onPageFinished()
解除进度对话框之前的方法中,您可以编写 10 秒延迟代码
That's it. Happy coding :)
就是这样。快乐编码:)
回答by CoolMind
According to Md. Sajedul Karimanswer I wrote a similar one.
根据Md. Sajedul Karim 的回答,我写了一个类似的。
webView = (WebView) view.findViewById(R.id.web);
progressBar = (ProgressBar) view.findViewById(R.id.progress);
webView.setWebChromeClient(new WebChromeClient());
setProgressBarVisibility(View.VISIBLE);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
setProgressBarVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
setProgressBarVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Toast.makeText(getActivity(), "Cannot load page", Toast.LENGTH_SHORT).show();
setProgressBarVisibility(View.GONE);
}
});
webView.loadUrl(url);
private void setProgressBarVisibility(int visibility) {
// If a user returns back, a NPE may occur if WebView is still loading a page and then tries to hide a ProgressBar.
if (progressBar != null) {
progressBar.setVisibility(visibility);
}
}
回答by Adrian Zelezniak
It's true, there are also more complete option, like changing the name of the app for a sentence you want. Check this tutorial it can help:
的确,还有更完整的选项,比如为你想要的句子更改应用程序的名称。检查本教程它可以帮助:
http://www.firstdroid.com/2010/08/04/adding-progress-bar-on-webview-android-tutorials/
http://www.firstdroid.com/2010/08/04/adding-progress-bar-on-webview-android-tutorials/
In that tutorial you have a complete example how to use the progressbar in a webview app.
在该教程中,您有一个完整的示例,说明如何在 webview 应用程序中使用进度条。
Adrian.
阿德里安。
回答by ninjahoahong
This is how I did it with Kotlin to show progress with percentage.
这就是我用 Kotlin 来显示百分比进度的方式。
My fragment layout.
我的片段布局。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/progressBar"/>
</FrameLayout>
My kotlin fragment in onViewCreated
我在 onViewCreated 中的 kotlin 片段
progressBar.max = 100;
webView.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
progressBar.progress = newProgress;
}
}
webView!!.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
progressBar.visibility = View.VISIBLE
progressBar.progress = 0;
super.onPageStarted(view, url, favicon)
}
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view?.loadUrl(request?.url.toString())
}
return true
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
progressBar.visibility = View.GONE
}
}
webView.loadUrl(url)
回答by idan
wait until the process is over ...
等到过程结束...
while(webview.getProgress()< 100){}
progressBar.setVisibility(View.GONE);
回答by Dhina k
webview= (WebView) findViewById(R.id.web);
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
WebActivity .setTitle("Loading...");
WebActivity .setProgress(progress * 100);
if(progress == 100)
WebActivity .setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://google.com");
For more reference click here http://androiddhina.blogspot.in/2015/05/android-load-webview-with-progressbar.html
如需更多参考,请点击此处http://androiddhina.blogspot.in/2015/05/android-load-webview-with-progressbar.html