一般移动加载H5时需要时间,所以这个时候经常加个Loading

常规的做法:

1.在webview刚开始的时候showloading,等WebViewClient回调onPageFinished的时候hide loading,

2.WebviewsetWebViewClient(mWebClient),在WebViewClientonPageStarted()回调中show loadingonPageFinished()中hide loading 

以上的做法正常情况下都没有问题,但是一旦遇到加载网页中出现重定向的时候onPageStarted()和onPageFinished() 会走多次,这个时候就会出现loading 闪烁问题

解决办法:其实在onPageFinished()的回调中可以通过webview拿到当前progress,根据progress进度来决定是否隐藏loading 

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.wv_web_view);
        mProgressBar = findViewById(R.id.pb_view);
        showProgressBar();
        initWebView();
        mWebView.loadUrl("https://www.baidu.com/");
    }  

    private void initWebView() {
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(mWebClient);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setInitialScale(25);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
    }

    private WebViewClient mWebClient = new WebViewClient() {

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

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.i("aaa", "onPageFinished  progress : " + view.getProgress());
            if (view.getProgress() == 100) {
                hideProgressBar();
            }
        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Log.i("aaa", "onReceivedError  error : " + error);
            hideProgressBar();
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            Log.i("aaa", "onReceivedSslError  error : " + error);
            hideProgressBar();
        }
    };

    public void showProgressBar() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }

    public void hideProgressBar() {
        if (mProgressBar != null) {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }


}

Demo 地址GitHub – JasonZhangHG/WebViewDemo: WebViewDemo

原文地址:https://blog.csdn.net/Jason_HD/article/details/126300848

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_10595.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注