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);
have your RecyclerView inside a FrameLayout or RelativeLayout with a TextView or other view with showing empty data message with visibility GONE by default and then in the adapter class, apply the logic
Here, I have one TextView with message no data
@Override
public int getItemCount() {
textViewNoData.setVisibility(data.size() > 0 ? View.GONE : View.VISIBLE);
return data.size();
}
You can just paint the text on the RecyclerView when it's empty. The following custom subclass supports empty, failed, loading, and offline modes. For successful compilation add recyclerView_stateText color to your resources.
/**
* {@code RecyclerView} that supports loading and empty states.
*/
public final class SupportRecyclerView extends RecyclerView
{
public enum State
{
NORMAL,
LOADING,
EMPTY,
FAILED,
OFFLINE
}
public SupportRecyclerView(@NonNull Context context)
{
super(context);
setUp(context);
}
public SupportRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
setUp(context);
}
public SupportRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setUp(context);
}
private Paint textPaint;
private Rect textBounds;
private PointF textOrigin;
private void setUp(Context c)
{
textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(ContextCompat.getColor(c, R.color.recyclerView_stateText));
textBounds = new Rect();
textOrigin = new PointF();
}
private State state;
public State state()
{
return state;
}
public void setState(State newState)
{
state = newState;
calculateLayout(getWidth(), getHeight());
invalidate();
}
private String loadingText = "Loading...";
public void setLoadingText(@StringRes int resId)
{
loadingText = getResources().getString(resId);
}
private String emptyText = "Empty";
public void setEmptyText(@StringRes int resId)
{
emptyText = getResources().getString(resId);
}
private String failedText = "Failed";
public void setFailedText(@StringRes int resId)
{
failedText = getResources().getString(resId);
}
private String offlineText = "Offline";
public void setOfflineText(@StringRes int resId)
{
offlineText = getResources().getString(resId);
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
String s = stringForCurrentState();
if (s == null)
return;
canvas.drawText(s, textOrigin.x, textOrigin.y, textPaint);
}
private void calculateLayout(int w, int h)
{
String s = stringForCurrentState();
if (s == null)
return;
textPaint.setTextSize(.1f * w);
textPaint.getTextBounds(s, 0, s.length(), textBounds);
textOrigin.set(
w / 2f - textBounds.width() / 2f - textBounds.left,
h / 2f - textBounds.height() / 2f - textBounds.top);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
calculateLayout(w, h);
}
private String stringForCurrentState()
{
if (state == State.EMPTY)
return emptyText;
else if (state == State.LOADING)
return loadingText;
else if (state == State.FAILED)
return failedText;
else if (state == State.OFFLINE)
return offlineText;
else
return null;
}
}
From my point of view the easiest way how to do an empty View is to create new empty RecyclerView with layout you want to inflate as a background.
And this empty Adapter is set when you check your dataset size.
I have fixed this:
Created layout layout_recyclerview_with_emptytext.xml file.
Created EmptyViewRecyclerView.java ---------
EmptyViewRecyclerView emptyRecyclerView = (EmptyViewRecyclerView) findViewById(R.id.emptyRecyclerViewLayout);
emptyRecyclerView.addAdapter(mPrayerCollectionRecyclerViewAdapter, "There is no prayer for selected category.");
It's an implementation of an AdapterDataObserver that allows you to simply set a View as the default empty layout for your RecylerView. This way, instead of using a custom RecyclerView and making your life harder, you can easily use it with your existing code:
Example Usage:
RVEmptyObserver observer = new RVEmptyObserver(recyclerView, emptyView)
rvAdapter.registerAdapterDataObserver(observer);
A more easy to use, and more resource save way to set Empty Label
of RecyclerView.
In short, a new View named RecyclerViewEmpty is introduced.
In its onDraw method, if the adapter is empty, it just paint an Empty Label
on its center, otherwise, continue super.onDraw();
class RecyclerViewEmpty extends RecyclerView {
....
@Override
public void onDraw(Canvas canvas) {
Adapter a = this.getAdapter();
if(a==null || a.getItemCount()<1) {
int x= (this.getWidth()-strWidth)>>1;
int y = this.getHeight()>>1 ;
canvas.drawText(this.emptyLabel, x, y, labelPaint);
}
else {
super.onDraw(canvas);
}
}
....
}