public class RecyclerLayout extends RelativeLayout implements View.OnClickListener {
private RecyclerView.Adapter mAdapter;
private View mLoadingView;
private View mEmptyView;
private View mErrorView;
private ShowState mState = ShowState.EMPTY;
private boolean forceModel = false;//若为true,不检查数据集是否为空
private OnClickListener onClickListener;
private RecyclerView.AdapterDataObserver dataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
if (mAdapter == null || mAdapter.getItemCount() <= 0) {
show(mState);
} else {
mState = ShowState.EMPTY;
hide();
}
}
};
public RecyclerLayout(Context context) {
super(context);
init(context);
}
public RecyclerLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public RecyclerLayout(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if (hasWindowFocus) {
if (getChildCount() > 0) {
View childView = getChildAt(0);
if (childView instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) childView;
mAdapter = recyclerView.getAdapter();
if (mAdapter != null) {
mAdapter.registerAdapterDataObserver(dataObserver);
}
if (mAdapter == null || mAdapter.getItemCount() <= 0) {
show(mState);
}
}
}
} else {
if (mAdapter != null) {
mAdapter.unregisterAdapterDataObserver(dataObserver);
}
}
}
private void showEmptyView() {
if (mEmptyView == null) {
mEmptyView = LayoutInflater.from(getContext()).inflate(R.layout.cm_layout_empty, null);
mEmptyView.setOnClickListener(this);
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(mEmptyView, layoutParams);
}
private void showErrorView() {
if (mErrorView == null) {
mErrorView = LayoutInflater.from(getContext()).inflate(R.layout.cm_layout_error, null);
mErrorView.setOnClickListener(this);
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(mErrorView, layoutParams);
}
private void showLoadView() {
if (mLoadingView == null) {
mLoadingView = LayoutInflater.from(getContext()).inflate(R.layout.cm_layout_loading, null);
mLoadingView.setOnClickListener(this);
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(mLoadingView, layoutParams);
}
/**
* 显示空布局、错误布局、加载布局
*
* @param state 显示类型
* @return 若显示返回true,不显示返回false
*/
public boolean show(ShowState state) {
if (!forceModel) {
if (mAdapter != null && mAdapter.getItemCount() > 0) {
return false;
}
}
if (state != mState) {
mState = state;
hide();
switch (state) {
case LOADING:
showLoadView();
break;
case EMPTY:
showEmptyView();
break;
case ERROR:
showErrorView();
break;
}
}
return true;
}
public void hide() {
if (getChildCount() > 2) {
throw new IllegalArgumentException("Child view must be only one!");
}
if (getChildCount() > 1) {
removeViewAt(1);
}
}
public void setEmptyText(String text) {
if (mEmptyView != null) {
((TextView) mEmptyView.findViewById(R.id.tv_empty)).setText(text);
}
}
public void setEmptyImage(int resId) {
if (mEmptyView != null) {
((ImageView) mEmptyView.findViewById(R.id.img_empty)).setImageResource(resId);
}
}
public void setLoadingText(String text) {
if (mLoadingView != null) {
((TextView) mLoadingView.findViewById(R.id.tv_loading)).setText(text);
}
}
public void setLoadingImage(int resId) {
if (mLoadingView != null) {
((ImageView) mLoadingView.findViewById(R.id.img_loading)).setImageResource(resId);
}
}
public void setErrorText(String text) {
if (mErrorView != null) {
((TextView) mErrorView.findViewById(R.id.tv_error)).setText(text);
}
}
public void setErrorImage(int resId) {
if (mErrorView != null) {
((ImageView) mErrorView.findViewById(R.id.img_error)).setImageResource(resId);
}
}
public void setEmptyView(int layoutId) {
mEmptyView = LinearLayout.inflate(getContext(), layoutId, null);
mEmptyView.setOnClickListener(this);
}
public void setErrorView(int layoutId) {
mErrorView = LinearLayout.inflate(getContext(), layoutId, null);
mErrorView.setOnClickListener(this);
}
public void setLoadingView(int layoutId) {
mLoadingView = LinearLayout.inflate(getContext(), layoutId, null);
mLoadingView.setOnClickListener(this);
}
public ShowState getState() {
return mState;
}
public boolean isForceModel() {
return forceModel;
}
public void setForceModel(boolean forceModel) {
this.forceModel = forceModel;
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
@Override
public void onClick(View v) {
if (onClickListener != null) {
onClickListener.onDisplayClick(RecyclerLayout.this, v, mState);
}
}
public interface OnClickListener {
void onDisplayClick(RecyclerLayout parentView, View childView, ShowState state);
}
public enum ShowState {
LOADING, EMPTY, ERROR
}
}