Rendering 1. GIF




Rendering 2. GIF

Implementation approach

The need is to show specific pages when there is no network, think of replacing pages, most of them are recyclerView or third-party recyclerview where the need to display data, so decided to replace all recyclerView pages for no network pages

The implementation process

  1. In BaseActivity, after the layout has been successfully loaded, the view to replace is found by id and passedindexOfChild()Method, find the position of the view to be replaced, and then implement by remove and Add View, because the replacement is based on the base, and use findviewById, this will not consider the view in the fragment, the limitation is that the name of the view to be replaced must be uniform

    2. Click the button to request data again, and the data needs to be refreshed again. Therefore, act without network pages needs to rewrite the data refreshing method of Base to request data again.

The advantages and disadvantages

Since only the base view is replaced, the id of the view to be replaced needs to be the same. This method is more efficient than the method of retrieving the content from the decorView through all the controls and eliminating the need to consider the fragment. The recyclerView that needs to be replaced in the Fragment can be obtained directly by using findViewById().

Code implementation

  1. The netless page was successfully initialized after loading the layout
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { initNoNetView(R.id.id_xrv,R.id.rv); }}Copy the code
  1. A utility class is required to obtain network state
** @param context * @return */ public static Boolean isConnected(context context) {ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (null ! = connectivity) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (null ! = info && info.isConnected()) { if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } return false; }Copy the code
  1. Initialize the netless page, define an array of view ids of variable length, parameter is the common ID of the view to be replaced, can be multiple, will replace the first found not null view.
private void initNoNetView(int ... ids) { if (! OkHttpUtil.isConnected(BaseActivity.this) && ! haveShowNetView) { final List<View> viewList = new ArrayList<>(); View for (int id: ids) {viewlist.add (findViewById(id)); } final View noNetView = view.inflate (BaseActivity. This, r.layout.layout_no_net, null); noNetView.findViewById(R.id.btn_try).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (! Okhttputil. isConnected(BaseActivity. This)) {t.howShort (BaseActivity. ); return; } showHaveNetView(viewList,noNetView); }}); ShowNoNetView (viewList,noNetView); }}Copy the code
  1. Page switching
private void showHaveNetView(List<View> viewList, View noNetView) {
        for (View view : viewList) {
            if (view==null){
                continue;
            }
            haveShowNetView = false;
            transView(noNetView, view);
            onReNetRefreshData();
            break;
        }
    }

    private void showNoNetView(List<View> viewList, View noNetView) {
        for (View view : viewList) {
            if (view==null){
                continue;
            }
            haveShowNetView = true;
            transView(view, noNetView);
            break;
        }
    }

protected void transView(final View defaultView, View replaceView) {
        final int index = ((ViewGroup) defaultView.getParent()).indexOfChild(defaultView);
        ViewGroup.LayoutParams params = defaultView.getLayoutParams();
        ViewGroup parent = (ViewGroup) defaultView.getParent();
        parent.removeView(defaultView);
        parent.addView(replaceView, index, params);
    }
Copy the code
  1. Click the button to notify the page to update data when there is a network page displayed. BaseAct is empty and the page that needs to update data is rewrittenonReNetRefreshData()Methods.
public void onReNetRefreshData() {

 }
Copy the code

The overall code

private boolean haveShowNetView = false; @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { initNoNetView(R.id.id_xrv,R.id.rv); } } private void initNoNetView(int ... ids) { if (! OkHttpUtil.isConnected(BaseActivity.this) && ! haveShowNetView) { final List<View> viewList = new ArrayList<>(); View for (int id: ids) {viewlist.add (findViewById(id)); } final View noNetView = view.inflate (BaseActivity. This, r.layout.layout_no_net, null); noNetView.findViewById(R.id.btn_try).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (! Okhttputil. isConnected(BaseActivity. This)) {t.howShort (BaseActivity. ); return; } showHaveNetView(viewList,noNetView); }}); ShowNoNetView (viewList,noNetView); } } private void showHaveNetView(List<View> viewList, View noNetView) { for (View view : viewList) { if (view==null){ continue; } haveShowNetView = false; transView(noNetView, view); onReNetRefreshData(); break; } } private void showNoNetView(List<View> viewList, View noNetView) { for (View view : viewList) { if (view==null){ continue; } haveShowNetView = true; transView(view, noNetView); break; } } public void onReNetRefreshData() { } protected void transView(final View defaultView, View replaceView) { final int index = ((ViewGroup) defaultView.getParent()).indexOfChild(defaultView); ViewGroup.LayoutParams params = defaultView.getLayoutParams(); ViewGroup parent = (ViewGroup) defaultView.getParent(); parent.removeView(defaultView); parent.addView(replaceView, index, params); }Copy the code