问题
I am using WebViewClient to load web pages and I would like to show progress bar for my code. My code isn't loading the progress gradually. It only shows 100% progress. How do I set the progress based on web page loading time?
For my purpose I have to load just "https://www.google.com" on a WebView only. Please help. Thank you. Code is shown below :
Activity file
public class MainActivity extends Activity {
private WebView webView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int vc = Color.parseColor("#feca01");
progressBar = (ProgressBar) findViewById(R.id.determinateBar);
progressBar.getProgressDrawable().setColorFilter(
vc, android.graphics.PorterDuff.Mode.SRC_IN);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://deelay.me/5000/http://www.google.com");
}
private class MyBrowser extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.incrementProgressBy(10);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.VISIBLE);
// progressBar.setProgress(0);
}
}
}
XML file
`
<ProgressBar
android:id="@+id/determinateBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="400dp"
android:padding="0dp"
android:layout_height="2dp" />
<WebView
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_weight="0.91"
android:layout_width="match_parent"
android:padding="0dp"
/>
`
回答1:
try this my friend
WebView webView;
ProgressBar progressBar;
webView = (WebView) findViewById(R.id.web);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setMax(100);
progressBar.setProgress(1);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
Layout
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:layout_marginTop="-7dp"
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:indeterminate="false"
android:max="100"
android:progress="1"
android:theme="@style/ProgressbarTheme" />
回答2:
Try using FinestWebView.
Using this you only need to load your webview by calling -
new FinestWebView.Builder(this).titleDefault("The Finest Artist")
.show("http://thefinestartist.com");
来源:https://stackoverflow.com/questions/44564808/show-progress-using-progressbar-for-webviewclient