Skip to content

Instantly share code, notes, and snippets.

@longkai
Created March 4, 2015 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longkai/e492b1543d99aa94a12e to your computer and use it in GitHub Desktop.
Save longkai/e492b1543d99aa94a12e to your computer and use it in GitHub Desktop.
A wrapped {@link android.support.v7.widget.RecyclerView} view with an empty view functionality like {@link android.widget.ListView}
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 longkai
*
* The software shall be used for good, not evil.
*/
package com.example.widget;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
/**
* A wrapped {@link android.support.v7.widget.RecyclerView} view with an empty view functionality like {@link android.widget.ListView}
*
* use this view you must provide an empty view whose view hierarchy is the same as this view and its initial visibility is GONE
*
* @author longkai
*/
public class EmptyRecyclerView extends RecyclerView {
private View mEmptyView;
/** If lazy, it means the first time the adapter attached with zero items, the empty view(if exist) won' t show */
private boolean mLazy = true;
private boolean mObserverAttached = false;
public EmptyRecyclerView(Context context) {
this(context, null);
}
public EmptyRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setEmptyView(View emptyView) {
mEmptyView = emptyView;
}
public void setLazy(boolean lazy) {
mLazy = lazy;
}
public void setEager(boolean eager) {
mLazy = !eager;
}
public boolean isLazy() {
return mLazy;
}
public boolean isEager() {
return !mLazy;
}
private void updateEmptyStatus(boolean empty) {
if (empty) {
setVisibility(GONE);
if (mEmptyView != null) {
mEmptyView.setVisibility(VISIBLE);
}
} else {
setVisibility(VISIBLE);
if (mEmptyView != null) {
mEmptyView.setVisibility(GONE);
}
}
}
public boolean isEmpty() {
Adapter adapter = getAdapter();
return adapter == null ? true : adapter.getItemCount() == 0;
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Adapter adapter = getAdapter();
if (adapter != null && mObserverAttached) {
adapter.unregisterAdapterDataObserver(mObserver);
mObserverAttached = false;
}
}
@Override public void setAdapter(Adapter adapter) {
super.setAdapter(adapter);
adapter.registerAdapterDataObserver(mObserver);
mObserverAttached = true;
if (isEager()) {
updateEmptyStatus(isEmpty());
}
}
private AdapterDataObserver mObserver = new AdapterDataObserver() {
@Override public void onChanged() {
super.onChanged();
updateEmptyStatus(isEmpty());
}
@Override public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
updateEmptyStatus(isEmpty());
}
@Override public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
updateEmptyStatus(isEmpty());
}
@Override public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
updateEmptyStatus(isEmpty());
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment