6

在 Android 应用程序中,我使用的是 TabView,其中一个选项卡显示了 WebView。但是在网页加载之前页面是空白的。在页面加载之前如何显示进度条?它不能在标题栏中,因为它被选项卡主机隐藏。

4

4 回答 4

17

我为此使用 ProgressBar。使用这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout [...]>

  <WebView 
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

  <ProgressBar 
       android:id="@+id/ProgressBar"
       android:layout_centerInParent="true"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       style="?android:attr/progressBarStyleLarge"
       android:visibility="gone"/>
</RelativeLayout>

我使用以下方法隐藏和显示进度指示器:

 WebView webView = (WebView) findViewById(R.id.WebView);

 final ProgressBar progess = (ProgressBar) findViewById(R.id.ProgressBar);

  webView.setWebViewClient(new WebViewClient() {
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
    progess.setVisibility(View.VISIBLE);
  }

  public void onPageFinished(WebView view, String url) {
    progess.setVisibility(View.GONE);
  }
}
于 2011-12-02T12:32:38.943 回答
2

Android 开发者网站上有一个非常好的教程。它展示了如何创建在整个 Android 程序中使用的“旋转轮”进度对话框,甚至介绍了如何在单独的线程中处理加载以防止应用程序在加载时冻结。

于 2010-03-22T22:39:47.807 回答
2

如果您的问题是“我如何知道页面何时加载?”,那么:

创建 WebViewClient 的自定义子类,覆盖 onPageFinished()

通过 setWebViewClient() 将 WebViewClient 子类的实例附加到 WebView

设置不定进度指示器(栏、对话框、RotateAnimation 等)

progressDialog = ProgressDialog.show(this, "", getText(R.string.progressDialogText), true);

在 WebView 上调用 loadUrl() 之前

让 onPageFinished() 摆脱进度指示器 (progressDialog.dismiss())

于 2010-09-02T09:39:43.113 回答
1

初始化视图后,只需调用此方法

startWebView(web_view,"Your Url");这里 web_view 是你初始化的 WebView 对象。下面startWebView()给出:

    private void startWebView(WebView webView,String url) {
 webView.setWebViewClient(new WebViewClient() {
            ProgressDialog progressDialog;

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            public void onLoadResource (WebView view, String url) {

                    if (progressDialog == null) {
                        progressDialog = new ProgressDialog(SponceredDetailsActivity.this);
                        progressDialog.setMessage("Loading...");
                        progressDialog.show();
                    }

            }
            public void onPageFinished(WebView view, String url) {
                try{
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                        progressDialog = null;
                    }

                }catch(Exception exception){
                    exception.printStackTrace();
                }
            }

        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);
    }

有时,如果 URL 死了,它会被重定向,并且会在 onPageFinished 方法之前到达 onLoadResource()。因此,进度条不会消失。要解决这个问题,请参阅我的这个答案

谢谢 :)

于 2016-03-21T15:50:12.093 回答