重構WebView 數據封裝 和 傳輸對象

前言:最近重構畢業設計,經過實習後,覺得以前寫的真的不算好,很多東西還可以優化,重構,代碼複用。現在寫的也不算漂亮,但是對自己整個大學生涯要有個總結。

添加加載進度條,WebView封裝成工具類,傳輸數據對象

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.WebViewActivity">
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/progressbar"
        android:max="100"
        android:visibility="visible"/>
    <WebView
        android:id="@+id/webView"
        android:layout_below="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/arrow_left"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_weight="1"
            android:background="@color/color_ffffff"
            app:srcCompat="@mipmap/arrow_left"/>

        <ImageButton
            android:id="@+id/arrow_right"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_weight="1"
            android:background="@color/color_ffffff"
            app:srcCompat="@mipmap/arrow_right"/>

    </LinearLayout>
</RelativeLayout>
public class WebViewUtil {
    @SuppressLint("JavascriptInterface")
    public void ClientWebView(final Activity mactive, WebView webView, final ProgressBar progressBar, String url){

        WebSettings webSettings = webView.getSettings();
        //支持js
        webSettings.setJavaScriptEnabled(true);
        // 是否可訪問本地文件,默認值 true
        webSettings.setAllowFileAccess(true);
        //自適應屏幕
        webSettings.setLoadWithOverviewMode(true);
        //動態更新內容
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //支持自動加載圖片
        webSettings.setLoadsImagesAutomatically(true);
        // 是否支持縮放
        webSettings.setSupportZoom(false);
        // 設置出現縮放工具
        webSettings.setBuiltInZoomControls(true);
        //擴大比例的縮放
        webSettings.setUseWideViewPort(true);
        //自適應屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        // 是否可用Javascript(window.open)打開窗口,默認值 false
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        // 設置可現實js的alert彈窗
        webView.setWebChromeClient(new WebChromeClient());

        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);
                }
            }});

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
                WebResourceResponse response = null;
                response =  super.shouldInterceptRequest(webView, webResourceRequest);
                return response;

            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, String s) {
                WebResourceResponse response = null;
                response =  super.shouldInterceptRequest(webView, s);
                String[] namelist = s.split("/");
                return response;
            }
        });
        webView.loadUrl(url);
    }
}

傳遞一個對象,該對象使用serializable序列化

public class WebUrl implements Serializable {
    String title;
    String url;

    public WebUrl(String title, String url) {
        this.title = title;
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

跳轉到WebViewActivity,攜帶對象

 skipWebView("兩學一做", "http://www.bhu.edu.cn/newsgl/page/lxyz/");
    private void skipWebView(String title, String url) {
        WebUrl webUrl = new WebUrl(title, url);
        Bundle bundle = new Bundle();
        bundle.putSerializable("WebUrl", webUrl);
        startActivity(WebViewActivity.class, bundle);
    }

WebViewActivity.class

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.webView)
    WebView webView;

    @Override
    protected int contentViewID() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initialize() {
        Intent intent = getIntent();
        WebUrl webUrl = (WebUrl) intent.getSerializableExtra("WebUrl");
        setTopTitle(webUrl.getTitle(), true);
        setLeftBtnFinish();
        WebViewUtil webViewUtil = new WebViewUtil();
        webViewUtil.ClientWebView(this,webView,progressBar,webUrl.getUrl());
    }

    @OnClick({R.id.arrow_left, R.id.arrow_right})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.arrow_left:
                if (webView.canGoBack()) {
                    webView.goBack();
                } else {
                    Toast.makeText(this, getString(R.string.tip_web_left),  Toast.LENGTH_LONG).show();
                    return;
                }
                break;
            case R.id.arrow_right:
                if (webView.canGoForward()) {
                    webView.goForward();
                } else {
                    Toast.makeText(this, getString(R.string.tip_web_right), Toast.LENGTH_LONG).show();
                    return;
                }
                break;
            default:
        }

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章