Android WebView进度条

时间:2010-03-29 11:23:45

标签: android progress-bar webview

我已经看过类似于here的问题但是因为我是新手,有人可以解释如何让它在WebView中工作或至少如何设置10秒的时间延迟让人们知道它正在加载?

8 个答案:

答案 0 :(得分:121)

对于水平进度条,首先需要定义进度条并将其与您的XML文件关联,如onCreate

final TextView txtview = (TextView)findViewById(R.id.tV1);
final ProgressBar pbar = (ProgressBar) findViewById(R.id.pB1);

然后,您可以在WebChromeClient中使用onProgressChanged方法:

MyView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
               if(progress < 100 && pbar.getVisibility() == ProgressBar.GONE){
                   pbar.setVisibility(ProgressBar.VISIBLE);
                   txtview.setVisibility(View.VISIBLE);
               }

               pbar.setProgress(progress);
               if(progress == 100) {
                   pbar.setVisibility(ProgressBar.GONE);
                   txtview.setVisibility(View.GONE);
               }
            }
        });

之后,在你的布局中你有类似的东西

<TextView android:text="Loading, . . ." 
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:id="@+id/tV1" android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textColor="#000000"></TextView>

<ProgressBar android:id="@+id/pB1"
    style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_centerVertical="true"
    android:padding="2dip">
</ProgressBar>

这就是我在我的应用中做到的方式。

答案 1 :(得分:21)

我刚刚在这里找到了一个非常好的例子: http://developer.android.com/reference/android/webkit/WebView.html。 您只需要更改setprogress:

activity.setProgress(progress * 1000);

activity.setProgress(progress * 100);

答案 2 :(得分:9)

这是在android Web View中添加进度条的最简单方法。

在您的activity / fragment

中添加一个布尔字段
private boolean isRedirected;

此布尔值将阻止网页重定向导致死链接。现在您可以将WebView对象和Web Url传递给此方法。

private void startWebView(WebView webView,String url) {

    webView.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog;

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

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

        public void onLoadResource (WebView view, String url) {
            if (!isRedirected) {
                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(SponceredDetailsActivity.this);
                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                }
            }

        }
        public void onPageFinished(WebView view, String url) {
            try{
                isRedirected=true;

                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }



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

    });

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

这里开始加载时会调用onPageStarted。这里我设置布尔字段是假的。但是当页面加载完成时,它将转到onPageFinished方法,这里布尔字段设置为true。有时,如果url已经死亡,它将重定向,并且会在onLoadResource()方法之前到达onPageFinished。因此,它不会隐藏进度条。为了防止这种情况,我要检查if (!isRedirected)

中的onLoadResource() 在解除进度对话框之前,在onPageFinished()方法中

您可以编写10秒延时代码

就是这样。快乐的编码:)

答案 3 :(得分:6)

根据 Md。 Sajedul Karim 回答我写了一篇类似的文章。

webView = (WebView) view.findViewById(R.id.web);
progressBar = (ProgressBar) view.findViewById(R.id.progress);
webView.setWebChromeClient(new WebChromeClient());

setProgressBarVisibility(View.VISIBLE);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        setProgressBarVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        setProgressBarVisibility(View.GONE);
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        Toast.makeText(getActivity(), "Cannot load page", Toast.LENGTH_SHORT).show();
        setProgressBarVisibility(View.GONE);
    }
});

webView.loadUrl(url);
private void setProgressBarVisibility(int visibility) {
    // If a user returns back, a NPE may occur if WebView is still loading a page and then tries to hide a ProgressBar.
    if (progressBar != null) {
        progressBar.setVisibility(visibility);
    }
}

答案 4 :(得分:4)

确实,还有更完整的选项,例如更改所需句子的应用名称。查看本教程可以提供帮助:

http://www.firstdroid.com/2010/08/04/adding-progress-bar-on-webview-android-tutorials/

在该教程中,您将获得如何在webview应用程序中使用进度条的完整示例。

阿德里安。

答案 5 :(得分:1)

这就是我与Kotlin一起完成的工作,以显示进度百分比。

我的片段布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

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

<ProgressBar
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/progressBar"/>
</FrameLayout>

我在onViewCreated中的kotlin片段

    progressBar.max = 100;
    webView.webChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            progressBar.progress = newProgress;
        }
    }

    webView!!.webViewClient = object : WebViewClient() {

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            progressBar.visibility = View.VISIBLE
            progressBar.progress = 0;
            super.onPageStarted(view, url, favicon)
        }

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            view?.loadUrl(url)
            return true
        }

        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?): Boolean {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                view?.loadUrl(request?.url.toString())
            }
            return true
        }

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            progressBar.visibility = View.GONE
        }
    }

    webView.loadUrl(url)

答案 6 :(得分:-1)

等到这个过程结束......

while(webview.getProgress()< 100){}
progressBar.setVisibility(View.GONE);

答案 7 :(得分:-1)

         webview= (WebView) findViewById(R.id.web);
         webview.setWebChromeClient(new WebChromeClient() 
         {
          public void onProgressChanged(WebView view, int progress)   
             {
                 WebActivity .setTitle("Loading...");
                 WebActivity .setProgress(progress * 100); 
                 if(progress == 100)
                 WebActivity .setTitle(R.string.app_name);
       }
     });
 webview.setWebViewClient(new WebViewClient());
 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadUrl("http://google.com");

如需更多参考,请点击此处http://androiddhina.blogspot.in/2015/05/android-load-webview-with-progressbar.html