android自定义空的view,Android 自定义空布局、Error布局

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值