Android 自定义空布局、Error布局

你不懂的神__经 · · 126 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
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
    }
}

本文来自:开源中国博客

感谢作者:你不懂的神__经

查看原文:Android 自定义空布局、Error布局

126 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet