加载Web视图后,如何使进度条消失?

时间:2019-01-18 05:32:36

标签: android webview

我正在为我的项目开发WebView。 我已将WebView与进度条一起使用。 加载WebView时,进度条将正确加载。加载页面后,进度条不会消失。 如何使其可见度消失。

我将分享代码。我已经创建了带有进度条的webview。

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = "https://rnbazar.com/";
    setContentView(R.layout.activity_main);
    final WebView myWebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    //myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.loadUrl(url);
    myWebView.setWebViewClient(new MyWebViewClient());

    myWebView.setWebViewClient(new WebViewClient() {
      @Override public void onReceivedError(WebView view, int errorCode,
        String description, String failingUrl) {
        myWebView.loadUrl("file:///android_asset/error.html");
      }
    });
  }

  private class MyWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(final WebView myWebView, final String url,
      final Bitmap favicon) {
      ProgressBar bar = (ProgressBar)
      myWebView.findViewById(R.id.progressBar);

      bar.setVisibility(View.VISIBLE);
      super.onPageStarted(myWebView, url, favicon);

    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView myWebView, String url) {

      myWebView.loadUrl(url);
      return true;
    }

    @Override
    public void onPageFinished(WebView myWebView, String url) {
      ProgressBar bar = (ProgressBar)
      myWebView.findViewById(R.id.progressBar);
      super.onPageFinished(myWebView, url);
      bar.setVisibility(View.GONE);
    }

  }
}

我想在页面加载后使消失进度栏。如果我再次单击任何链接,则加载程序会出现,并在页面加载后再次消失。

1 个答案:

答案 0 :(得分:1)

一个WebView可能不包含任何R.id.progressBar ...

public class MainActivity extends Activity {

    private WebView mWebView = null;
    private Progressbar mProgressbar = null;
    private String url = "https://rnbazar.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        this.mWebView = (WebView) findViewById(R.id.webview);
        this.mProgressbar = (Progressbar) findViewById(R.id.progressBar);
    }
    ...

然后您可以使用该引用:

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView webView, String url,  Bitmap favicon) {
        super.onPageStarted(webView, url, favicon);
        mProgressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        webView.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView webView, String url) {
        super.onPageFinished(webView, url);
        mProgressbar.setVisibility(View.GONE);
    }
}