我在android webview中添加了进度条,虽然运行项目进度条效果很好,但是问题是即使加载网站后进度条也不会停止。
这是我尝试的代码。
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView2);
bar =(ProgressBar) findViewById(R.id.progressBar2);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://florro-001-site8.etempurl.com");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
}
public class myWebClient extends WebViewClient{
@Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:1)
设置可见性进入
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
bar.setVisibility(View.GONE);
return super.shouldOverrideUrlLoading(view, url);
}
您还创建了myWebClient
类,但是您忘记设置了
替换此行
webView.setWebViewClient(new WebViewClient());
收件人
webView.setWebViewClient(new myWebClient());
答案 1 :(得分:0)
您创建了内部类myWebClient
,在其中您更改了可见性,但从未将其设置为Webview对象。
在onCreate()
方法中:
webView.setWebViewClient(new WebViewClient());
删除2行webView.setWebViewClient(new WebViewClient());