欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 仿微信Webview加载网页进度和失败重试

程序员文章站 2022-07-14 15:09:48
...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/WHFull"
    android:orientation="vertical"
    android:background="@color/white">

    <cn.ouyu.easeui.widget.TitleBar
        style="@style/WFullHAuto"
        app:TBCenterText="帮助"
        app:TBIsTitleMustMiddle="true"
        android:id="@+id/tbTitle"/>

    <ProgressBar
        android:id="@+id/pb"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:progressDrawable="@drawable/progressbar_color_red"
        android:layout_width="match_parent"
        android:layout_height="3dp"/>

    <FrameLayout style="@style/WFullHAutoFull" android:id="@+id/flContent">

        <WebView style="@style/WHFull"
                 android:overScrollMode="never"
            android:id="@+id/wvContent"/>

        <LinearLayout
            android:id="@+id/llError"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            android:gravity="center_horizontal"
            android:orientation="vertical">
            <ImageView
                android:layout_width="@dimen/dp40"
                android:src="@drawable/icon_reload"
                android:layout_height="@dimen/dp40"/>
            <TextView
                android:layout_width="wrap_content"
                android:text="无法打开页面"
                android:layout_marginTop="@dimen/dp20"
                android:layout_height="wrap_content"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/dp12"
                android:layout_marginTop="@dimen/dp10"
                android:text="轻触屏幕重新加载"/>
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

1.进度条样式progressbar_color_green

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 背景  gradient是渐变,corners定义的是圆角 -->
    <item android:id="@android:id/background">
        <shape>
            <!--<corners android:radius="@dimen/dp6" />-->
            <solid android:color="@color/white" />
        </shape>
    </item>
    <!--&lt;!&ndash; 第二条进度条颜色 &ndash;&gt;-->
    <!--<item android:id="@android:id/secondaryProgress">-->
    <!--<clip>-->
    <!--<shape>-->
    <!--<corners android:radius="10dip" />-->
    <!--<gradient-->
    <!--android:angle="90.0"-->
    <!--android:centerColor="#ac6079"-->
    <!--android:centerY="0.45"-->
    <!--android:endColor="#6c213a"-->
    <!--android:startColor="#e71a5e" />-->
    <!--</shape>-->
    <!--</clip>-->
    <!--</item>-->
    <!-- 进度条 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:bottomRightRadius="@dimen/dp6" android:topRightRadius="@dimen/dp6"/>
                <solid android:color="@color/colorAccent" />
            </shape>
        </clip>
    </item>
</layer-list>

2.逻辑代码:

 private boolean isError;  
/**
     * 初始化控件
     */
    private void initViews() {
        wvContent.getSettings().setSupportZoom(true);
        wvContent.getSettings().setBuiltInZoomControls(true);
        wvContent.getSettings().setDisplayZoomControls(false);
        wvContent.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wvContent.getSettings().setAppCacheEnabled(false);
        wvContent.getSettings().setJavaScriptEnabled(true);
        wvContent.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // webview自己加载URL,让后通知系统不需要HandleURL
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!isError){
                    wvContent.setVisibility(View.VISIBLE);
                }
            }

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

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                    mLlError.setVisibility(View.VISIBLE);
                    wvContent.setVisibility(View.INVISIBLE);
                    isError = true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    return;
                }
                mLlError.setVisibility(View.VISIBLE);
                wvContent.setVisibility(View.INVISIBLE);
                isError = true;
            }
        });

        wvContent.setWebChromeClient(new WebChromeClient() {
            /**
             * 进度网页变化
             */
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mPb.setProgress(newProgress);
                if (newProgress == 100) {
                    mPb.setVisibility(View.GONE);
                }
            }

            /**
             * 获取网页标题
             */
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                tvTitle.setText(title);
            }

            /**
             * 获取网页图标
             */
            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
//                ivLogo.setImageBitmap(icon);
            }

            /**
             * 解决webview定位问题
             * @param origin
             * @param callback
             */
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }
        });
    }
@Override
    protected void onResume() {
        super.onResume();
        if (wvContent != null) {
            wvContent.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (wvContent != null) {
            wvContent.onPause();
        }
    }

    @Override
    public void onBackPressed() {
        if (wvContent.canGoBack()) {
            wvContent.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        wvContent.clearCache(true);
        wvContent.removeAllViews();
        wvContent.destroy();
        wvContent = null;
        super.onDestroy();
    }

 @OnClick(R.id.flContent)
    public void onViewClicked() {
        wvContent.reload();
        isError = false;
    }

 

 

 

 

 

 

 

 

 

 

相关标签: android相关