Equivalent of ListView.setEmptyView in RecyclerView

前端 未结 13 1681
孤街浪徒
孤街浪徒 2020-11-30 17:44

In RecyclerView, I want to set an empty view to be shown when the adapter is empty. Is there an equivalent of ListView.setEmptyView()?

13条回答
  •  Happy的楠姐
    2020-11-30 18:04

    If you want to support more states such as loading state, error state then you can checkout https://github.com/rockerhieu/rv-adapter-states. Otherwise supporting empty view can be implemented easily using RecyclerViewAdapterWrapper from (https://github.com/rockerhieu/rv-adapter). The main advantage of this approach is you can easily support empty view without changing the logic of the existing adapter:

    public class StatesRecyclerViewAdapter extends RecyclerViewAdapterWrapper {
        private final View vEmptyView;
    
        @IntDef({STATE_NORMAL, STATE_EMPTY})
        @Retention(RetentionPolicy.SOURCE)
        public @interface State {
        }
    
        public static final int STATE_NORMAL = 0;
        public static final int STATE_EMPTY = 2;
    
        public static final int TYPE_EMPTY = 1001;
    
        @State
        private int state = STATE_NORMAL;
    
        public StatesRecyclerViewAdapter(@NonNull RecyclerView.Adapter wrapped, @Nullable View emptyView) {
            super(wrapped);
            this.vEmptyView = emptyView;
        }
    
        @State
        public int getState() {
            return state;
        }
    
        public void setState(@State int state) {
            this.state = state;
            getWrappedAdapter().notifyDataSetChanged();
            notifyDataSetChanged();
        }
    
        @Override
        public int getItemCount() {
            switch (state) {
                case STATE_EMPTY:
                    return 1;
            }
            return super.getItemCount();
        }
    
        @Override
        public int getItemViewType(int position) {
            switch (state) {
                case STATE_EMPTY:
                    return TYPE_EMPTY;
            }
            return super.getItemViewType(position);
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch (viewType) {
                case TYPE_EMPTY:
                    return new SimpleViewHolder(vEmptyView);
            }
            return super.onCreateViewHolder(parent, viewType);
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            switch (state) {
                case STATE_EMPTY:
                    onBindEmptyViewHolder(holder, position);
                    break;
                default:
                    super.onBindViewHolder(holder, position);
                    break;
            }
        }
    
        public void onBindEmptyViewHolder(RecyclerView.ViewHolder holder, int position) {
        }
    
        public static class SimpleViewHolder extends RecyclerView.ViewHolder {
            public SimpleViewHolder(View itemView) {
                super(itemView);
            }
        }
    }
    

    Usage:

    Adapter adapter = originalAdapter();
    StatesRecyclerViewAdapter statesRecyclerViewAdapter = new StatesRecyclerViewAdapter(adapter, emptyView);
    rv.setAdapter(endlessRecyclerViewAdapter);
    
    // Change the states of the adapter
    statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_EMPTY);
    statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_NORMAL);
    

提交回复
热议问题