给Webview添加加载进度条

Android AUmaWSsSoi 3个月前 (06-29) 97次浏览 0个评论
文章目录[隐藏]

给Webview添加加载进度条

大家用微信看订阅号应该注意到了每次加载一篇文章,actionbar下面就有一条绿色的线,来显示加载的进度,那么如何给我们自己的app也添加上这样的功能呢,这里我们来实际操作一下。

核心代码

先说一下思路,自定义一个ViewGroup,可以是继承自FrameLayout或者是LinearLayout,添加progressBar(用来显示进度)和WebView(加载url)

下面是代码

private void init(Context context) {
    mContext = context;
    mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progress_horizontal, null);
    mProgressBar.setMax(100);
    mProgressBar.setProgress(0);
    mWebView = new WebView(context);
    //WebView.getSettings().setJavaScriptEnabled(true);// settings相关操作
    LayoutParams lps = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    addView(mWebView, lps);
    mWebView.setWebChromeClient(commonChromeClient);
    mWebView.setWebViewClient(commonClient);
    addView(mProgressBar, LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mBarHeight, getResources().getDisplayMetrics()));
}

需要注意的是在WebViewClient的几个实现方法中,以及WebChromeClient中,处理progressBar的显示隐藏。

class CommonWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Uri uri = Uri.parse(url);
            if (TextUtils.isEmpty(uri.getScheme()) || !uri.getScheme().equals("http") && !uri.getScheme().equals("https"))
                return true;
            return super.shouldOverrideUrlLoading(view, url);
        }

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

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

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            // 不要使用super,否则有些手机访问不了,因为包含了一条 handler.cancel()
            // super.onReceivedSslError(view, handler, error);
            // 接受所有网站的证书,忽略SSL错误,执行访问网页
            handler.proceed();
        }
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            return super.shouldInterceptRequest(view, request);
        }
    }
class CommonWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        mProgressBar.setProgress(newProgress);
        if (newProgress == 100) {
            mProgressBar.setVisibility(View.GONE);
        } else {
            mProgressBar.setProgress(newProgress);
            mProgressBar.setVisibility(VISIBLE);
        }
        super.onProgressChanged(view, newProgress);
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
    }

    @Override
    public void onReceivedTouchIconUrl(WebView view, String url,
                                       boolean precomposed) {
        super.onReceivedTouchIconUrl(view, url, precomposed);
    }

    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog,
                                  boolean isUserGesture, Message resultMsg) {
        return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
        super.onGeolocationPermissionsShowPrompt(origin, callback);
    }
}

以上就是核心代码了。最终的效果就跟微信加载文章的效果差不多了。


java问与答 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:http://javaexception.com/archives/160
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址