Created
December 9, 2016 09:14
-
-
Save baleen37/20d1b43f1de1f2550cfb152a2e4f88c5 to your computer and use it in GitHub Desktop.
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