Created
February 1, 2016 15:26
-
-
Save RowlandOti/eb8146a522217f618305 to your computer and use it in GitHub Desktop.
Solution of RecyclerView with setEmptyView method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RecyclerViewEmptySupport extends RecyclerView { | |
private View emptyView; | |
private AdapterDataObserver emptyObserver = new AdapterDataObserver() { | |
@Override | |
public void onChanged() { | |
Adapter<?> adapter = getAdapter(); | |
if(adapter != null && emptyView != null) { | |
if(adapter.getItemCount() == 0) { | |
emptyView.setVisibility(View.VISIBLE); | |
RecyclerViewEmptySupport.this.setVisibility(View.GONE); | |
} | |
else { | |
emptyView.setVisibility(View.GONE); | |
RecyclerViewEmptySupport.this.setVisibility(View.VISIBLE); | |
} | |
} | |
} | |
}; | |
public RecyclerViewEmptySupport(Context context) { | |
super(context); | |
} | |
public RecyclerViewEmptySupport(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public RecyclerViewEmptySupport(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
public void setAdapter(Adapter adapter) { | |
super.setAdapter(adapter); | |
if(adapter != null) { | |
adapter.registerAdapterDataObserver(emptyObserver); | |
} | |
emptyObserver.onChanged(); | |
} | |
public void setEmptyView(View emptyView) { | |
this.emptyView = emptyView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment