空頁面的顯示很常用,所以自己做了一個通用的空頁面顯示,先看效果圖 在有網路的時候正常載入顯示,在沒有網路的時候自動載入空頁面,點擊空頁面重新載入網路請求的一個功能 1:定義一個xml頁面,頁面佈局是一個iamgeview和一個textview的顯示 2:添加輔助類,控制載入空頁面和顯示隱藏等邏輯 3 ...
空頁面的顯示很常用,所以自己做了一個通用的空頁面顯示,先看效果圖
在有網路的時候正常載入顯示,在沒有網路的時候自動載入空頁面,點擊空頁面重新載入網路請求的一個功能
1:定義一個xml頁面,頁面佈局是一個iamgeview和一個textview的顯示
2:添加輔助類,控制載入空頁面和顯示隱藏等邏輯
3:在網路回調的時候調用方法顯示以及在退出的時候清空緩存
下麵是代碼

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/img_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/empty" /> <TextView android:id="@+id/tv_val" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/d51.0" android:text="網路開小差了,點擊屏幕重試" android:textSize="@dimen/d43.0" /> </LinearLayout>View Code

package com.fragmentapp.helper; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.fragmentapp.R; /** * Created by liuzhen on 2017/12/28. */ public class EmptyLayout { private Context context; private FrameLayout root; private TextView tv_val; private CallBack callBack; public EmptyLayout(Context context){ this.context = context; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); params.gravity = Gravity.CENTER; root = new FrameLayout(context); root.setLayoutParams(params); View view = LayoutInflater.from(context).inflate(R.layout.layout_error,null); tv_val = view.findViewById(R.id.tv_val); root.addView(view); } public void showEmpty(final ViewGroup group, String val){ tv_val.setText(val); group.removeView(root); group.addView(root,root.getLayoutParams()); status(group,true); root.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (callBack != null) callBack.click(); status(group,false); } }); } private void status(ViewGroup group,boolean isShow){ if (isShow){ for (int i = 0;i < group.getChildCount();i++) { View view = group.getChildAt(i); view.setVisibility(View.GONE); } root.setVisibility(View.VISIBLE);//放在後面才能生效 }else{ for (int i = 0;i < group.getChildCount();i++) { View view = group.getChildAt(i); view.setVisibility(View.VISIBLE); } root.setVisibility(View.GONE); } } public void setCallBack(CallBack callBack){ if (this.callBack == null) this.callBack = callBack; } public void clear(){ context = null; root.removeAllViews(); root = null; callBack = null; } public interface CallBack{ void click(); } }View Code
這裡有幾個註意的地方特別講解一下,就是空頁面的初始化和退出時候的一個調用
空頁面的初始化和退出因為是必須的一個操作,所以放在了base裡面去控制
visible方法是上篇文章里說的懶載入,頁面只會載入一次,而空頁面也是一樣,讓它只在初始化的時候創建一個對象
然後是退出時候調用
就是清除了我們的資源,釋放記憶體
接下來就是在網路請求錯誤的地方調用我們的方法就行了
網路錯誤空頁面將顯示,點擊空頁面重新調用載入方法,如果在次網路錯誤,則在次調用顯示我們的空頁面
這樣就把多餘的邏輯控制都封裝在輔助類裡面了,而實際上我們需要考慮手動調用的就只有一行代碼而已,其它的都不需要去考慮
歡迎反饋意見,歡迎收藏^_^
GitHub:https://github.com/1024477951/FragmentApp