即使将网站加载到android webview中,进度条也不会停止

时间:2019-07-12 10:07:37

标签: android webview progress-bar

我在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);
    }
}

2 个答案:

答案 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());