Skip to content

Instantly share code, notes, and snippets.

@meoyawn
Created November 1, 2014 11:20
Show Gist options
  • Select an option

  • Save meoyawn/31c8f054d1af4588dc5c to your computer and use it in GitHub Desktop.

Select an option

Save meoyawn/31c8f054d1af4588dc5c to your computer and use it in GitHub Desktop.
RecyclerView doesn't have an emptyView support, we gotta fix that
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class EmptyRecyclerView extends RecyclerView {
@Nullable View emptyView;
public EmptyRecyclerView(Context context) { super(context); }
public EmptyRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }
public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
void checkIfEmpty() {
if (emptyView != null) {
emptyView.setVisibility(getAdapter().getItemCount() > 0 ? GONE : VISIBLE);
}
}
final @NotNull AdapterDataObserver observer = new AdapterDataObserver() {
@Override public void onChanged() {
super.onChanged();
checkIfEmpty();
}
};
@Override public void setAdapter(@Nullable Adapter adapter) {
final Adapter oldAdapter = getAdapter();
if (oldAdapter != null) {
oldAdapter.unregisterAdapterDataObserver(observer);
}
super.setAdapter(adapter);
if (adapter != null) {
adapter.registerAdapterDataObserver(observer);
}
}
public void setEmptyView(@Nullable View emptyView) {
this.emptyView = emptyView;
checkIfEmpty();
}
}
@meoyawn

meoyawn commented Sep 6, 2015

Copy link
Copy Markdown
Author

@mobiRic you can do whatever you want with this code :)

@mobiRic

mobiRic commented Sep 7, 2015

Copy link
Copy Markdown

Thanks - forked to https://gist.github.com/mobiRic/963a814d51259c730467

Can I suggest adding a method for swapAdapter() for completeness:

@Override
public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) {
    final Adapter oldAdapter = getAdapter();
    if (oldAdapter != null) {
        oldAdapter.unregisterAdapterDataObserver(observer);
    }

    if (adapter != null) {
        adapter.registerAdapterDataObserver(observer);
    }
    super.swapAdapter(adapter, removeAndRecycleExistingViews);
    checkIfEmpty();
}

@sevar83

sevar83 commented Sep 11, 2015

Copy link
Copy Markdown

@AnirudhaAgashe suggestion is correct!

@mmanishh

mmanishh commented Oct 9, 2015

Copy link
Copy Markdown

@notnul coudnl't resolve

@bryant1410

Copy link
Copy Markdown

maybe the emptyView attribute could be private

@subbuboyapati

Copy link
Copy Markdown

Hi @adelnizamutdinov , This we can achieve in flowing way.
Make two list item one for showing normal data and another one is for empty view

  1. In getitemcount, if count is zero return 1 otherwise size of an adapter.
  2. In itemViewType if size 0 return empty view type otherwise return your normal list item.

@Ghostish

Copy link
Copy Markdown

Really appreciate this great idea . It works well.

@Honghe

Honghe commented Jul 13, 2016

Copy link
Copy Markdown

adapter.notifyItemRemoved(position); will not evoke checkIfEmpty()

@wangyiyong

Copy link
Copy Markdown

But where is the emptyView attached?

@beraybentesen

Copy link
Copy Markdown

Does nothing when I use following method :
recyclerView.postDelayed(() -> Notify RecyclerView

Inflated like this :
View headerView = View.inflate(getActivity(), R.layout.layout_empty_view, null);

@josemigallas

josemigallas commented Oct 13, 2017

Copy link
Copy Markdown

@adelnizamutdinov
Hi, I think there's a small typo on line 25. Shouldn't it be @NonNull instead of @NotNull?

@josemigallas

Copy link
Copy Markdown

It is also not working without implementing onItemRangeInserted in the observer. I'm using FirebaseUI.

@Blablablar

Copy link
Copy Markdown

I have the same question, where is the emptyView attached?

@SohailZahidGit

Copy link
Copy Markdown

Its good but what happened if we had to add one more observer to for lazy loading/paging?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment