首先需要自定义一个状态StateFrameLayout布局,它是继承FrameLayout。在这个类中,目前是设置五种不同状态的视图布局,主要的功能操作是显示或者隐藏布局。为了后期代码维护性,根据面向对象的思想,类尽量保证单一职责,所以关于状态切换,以及设置自定义状态布局,把这个功能分离处理,放到一个StateLayoutManager中处理。
- 看代码可知,这个类的功能非常明确,就是隐藏或者展示视图作用。
/**
* <pre>
* @author yangchong
* blog : https://github.com/yangchong211/YCStateLayout
* time : 2017/7/6
* desc : 自定义帧布局
* revise:
* </pre>
*/
public class StateFrameLayout extends FrameLayout {
/**
* loading 加载id
*/
public static final int LAYOUT_LOADING_ID = 1;
/**
* 内容id
*/
public static final int LAYOUT_CONTENT_ID = 2;
/**
* 异常id
*/
public static final int LAYOUT_ERROR_ID = 3;
/**
* 网络异常id
*/
public static final int LAYOUT_NETWORK_ERROR_ID = 4;
/**
* 空数据id
*/
public static final int LAYOUT_EMPTY_DATA_ID = 5;
/**
* 存放布局集合
*/
private SparseArray<View> layoutSparseArray = new SparseArray<>();
//private HashMap<Integer,View> map = new HashMap<>();
/**
* 布局管理器
*/
private StateLayoutManager mStatusLayoutManager;
public StateFrameLayout(Context context) {
super(context);
}
public StateFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StateFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setStatusLayoutManager(StateLayoutManager statusLayoutManager) {
mStatusLayoutManager = statusLayoutManager;
//添加所有的布局到帧布局
addAllLayoutToRootLayout();
}
private void addAllLayoutToRootLayout() {
if (mStatusLayoutManager.contentLayoutResId != 0) {
addLayoutResId(mStatusLayoutManager.contentLayoutResId, StateFrameLayout.LAYOUT_CONTENT_ID);
}
if (mStatusLayoutManager.loadingLayoutResId != 0) {
addLayoutResId(mStatusLayoutManager.loadingLayoutResId, StateFrameLayout.LAYOUT_LOADING_ID);
}
if (mStatusLayoutManager.emptyDataVs != null) {
addView(mStatusLayoutManager.emptyDataVs);
}
if (mStatusLayoutManager.errorVs != null) {
addView(mStatusLayoutManager.errorVs);
}
if (mStatusLayoutManager.netWorkErrorVs != null) {
addView(mStatusLayoutManager.netWorkErrorVs);
}
}
private void addLayoutResId(@LayoutRes int layoutResId, int id) {
View resView = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResId, null);
layoutSparseArray.put(id, resView);
addView(resView);
}
/**
* 显示loading
*/
public void showLoading() {
if (layoutSparseArray.get(LAYOUT_LOADING_ID) != null) {
showHideViewById(LAYOUT_LOADING_ID);
}
}
/**
* 显示内容
*/
public void showContent() {
if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null) {
showHideViewById(LAYOUT_CONTENT_ID);
}
}
/**
* 显示空数据
*/
public void showEmptyData(int iconImage, String textTip) {
if (inflateLayout(LAYOUT_EMPTY_DATA_ID)) {
showHideViewById(LAYOUT_EMPTY_DATA_ID);
emptyDataViewAddData(iconImage, textTip);
}
}
/**
* 根据ID显示隐藏布局
* @param id id值
*/
private void showHideViewById(int id) {
for (int i = 0; i < layoutSparseArray.size(); i++) {
int key = layoutSparseArray.keyAt(i);
View valueView = layoutSparseArray.valueAt(i);
//显示该view
if(key == id) {
valueView.setVisibility(View.VISIBLE);
if(mStatusLayoutManager.onShowHideViewListener != null) {
mStatusLayoutManager.onShowHideViewListener.onShowView(valueView, key);
}
} else {
if(valueView.getVisibility() != View.GONE) {
valueView.setVisibility(View.GONE);
if(mStatusLayoutManager.onShowHideViewListener != null) {
mStatusLayoutManager.onShowHideViewListener.onHideView(valueView, key);
}
}
}
}
}
/**
* 这个是处理ViewStub的逻辑,主要有网络异常布局,加载异常布局,空数据布局
* @param id 布局id
* @return 布尔值
*/
private boolean inflateLayout(int id) {
boolean isShow = true;
//如果为null,则直接返回false
if (layoutSparseArray.get(id) == null) {
return false;
}
switch (id) {
case LAYOUT_NETWORK_ERROR_ID:
if (mStatusLayoutManager.netWorkErrorVs != null) {
View view = mStatusLayoutManager.netWorkErrorVs.inflate();
retryLoad(view, mStatusLayoutManager.netWorkErrorRetryViewId);
layoutSparseArray.put(id, view);
isShow = true;
} else {
isShow = false;
}
break;
case LAYOUT_ERROR_ID:
if (mStatusLayoutManager.errorVs != null) {
View view = mStatusLayoutManager.errorVs.inflate();
if (mStatusLayoutManager.errorLayout != null) {
mStatusLayoutManager.errorLayout.setView(view);
}
retryLoad(view, mStatusLayoutManager.errorRetryViewId);
layoutSparseArray.put(id, view);
isShow = true;
} else {
isShow = false;
}
break;
case LAYOUT_EMPTY_DATA_ID:
if (mStatusLayoutManager.emptyDataVs != null) {
View view = mStatusLayoutManager.emptyDataVs.inflate();
if (mStatusLayoutManager.emptyDataLayout != null) {
mStatusLayoutManager.emptyDataLayout.setView(view);
}
retryLoad(view, mStatusLayoutManager.emptyDataRetryViewId);
layoutSparseArray.put(id, view);
isShow = true;
} else {
isShow = false;
}
break;
default:
break;
}
return isShow;
}
}