加载 WebView 时的 Android ProgressBar [英] Android ProgessBar while loading WebView

查看:23
本文介绍了加载 WebView 时的 Android ProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个 WebView 可以从 Internet 加载任何 URL.现在,有时由于网络速度较慢,页面加载时间较长,用户只能看到空白屏幕.

In my application, I have a WebView which loads any URL from the internet. Now, sometimes due to slow networks the page takes a long time to load and the user sees only a blank screen.

我想在 WebView 加载时显示 ProgressBar 并在 WebView 获取时隐藏 ProgessBar已完全加载.

I want to show a ProgressBar while the WebView gets loaded and hide the ProgessBar when the WebView gets loaded completely.

我知道如何使用 ProgressBarAsyncTasks,但这是我的问题.

I know how to use the ProgressBar and AsyncTasks, but here is my problem.

这是我用来加载我的 WebView 的代码.

This is the code that I use to load my WebView.

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl(web_URL);

这是我的自定义 WebViewClient

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

现在,如果我尝试使用 AsyncTasks显示 ProgressBar 那么我想我将不得不提供代码来加载 URL在我的 AsyncTaskdoInBackGround() 函数中,并通过 onProgressUpdate() 函数显示进度.

Now, if I try to show the ProgressBar using AsyncTasks then I guess I would have to give the code to load the URL in the doInBackGround() function of my AsyncTask and show the progress through the onProgressUpdate() function.

但是,我如何在 doInBackground() 中加载 URL,因为 doInBackground()非 UI 线程 上运行,我将无法使用 mWebView.loadUrl(web_URL) 在里面.

But, how do I load the URL inside the doInBackground() as doInBackground() runs on the Non-UI thread and I wont be able to use mWebView.loadUrl(web_URL) inside it.

有什么建议吗?我错过了一些明显的东西吗?请指导我.

Any suggestions? Am I missing something obvious? Please guide me.

推荐答案

查看源代码.帮您解决问题...

Check the source code. Help you and solve your problem...

public class AppWebViewClients extends WebViewClient {
     private ProgressBar progressBar;

    public AppWebViewClients(ProgressBar progressBar) {
        this.progressBar=progressBar;
        progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
}

我认为它对你有帮助.

谢谢.

这篇关于加载 WebView 时的 Android ProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆