Created
August 29, 2016 04:32
-
-
Save xEdenx/ae580833fd4948343dc934da739b4240 to your computer and use it in GitHub Desktop.
Recyclerview with emptyview
This file contains 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
package com.tneciv.blueprint.widget; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* Created by Tneciv | |
* on 2016-08-20 01:13 . | |
* https://github.com/googlesamples/android-XYZTouristAttractions/blob/master/Application/src/main/java/com/example/android/xyztouristattractions/ui/AttractionsRecyclerView.java | |
*/ | |
public class BluePrintRecyclerView extends RecyclerView { | |
private View mEmptyView; | |
private AdapterDataObserver mDataObserver = new AdapterDataObserver() { | |
@Override | |
public void onChanged() { | |
super.onChanged(); | |
updateEmptyView(); | |
} | |
}; | |
public BluePrintRecyclerView(Context context) { | |
super(context); | |
} | |
public BluePrintRecyclerView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public BluePrintRecyclerView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
/** | |
* Designate a view as the empty view. When the backing adapter has no | |
* data this view will be made visible and the recycler view hidden. | |
*/ | |
public void setEmptyView(View emptyView) { | |
mEmptyView = emptyView; | |
} | |
@Override | |
public void setAdapter(RecyclerView.Adapter adapter) { | |
if (getAdapter() != null) { | |
getAdapter().unregisterAdapterDataObserver(mDataObserver); | |
} | |
if (adapter != null) { | |
adapter.registerAdapterDataObserver(mDataObserver); | |
} | |
super.setAdapter(adapter); | |
updateEmptyView(); | |
} | |
private void updateEmptyView() { | |
if (mEmptyView != null && getAdapter() != null) { | |
boolean showEmptyView = getAdapter().getItemCount() == 0; | |
mEmptyView.setVisibility(showEmptyView ? VISIBLE : GONE); | |
setVisibility(showEmptyView ? GONE : VISIBLE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment