Created
September 27, 2016 10:08
-
-
Save zplesac/c0a2ad73197e1c0bba773c5d6b090822 to your computer and use it in GitHub Desktop.
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 co.infinum.bigbetworld.views; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.view.View; | |
/** | |
* Created by Željko Plesac on 12/09/16. | |
*/ | |
public class BBWRecyclerView extends RecyclerView { | |
private View emptyView; | |
private final AdapterDataObserver observer = new AdapterDataObserver() { | |
@Override | |
public void onChanged() { | |
checkIfEmpty(); | |
} | |
@Override | |
public void onItemRangeInserted(int positionStart, int itemCount) { | |
checkIfEmpty(); | |
} | |
@Override | |
public void onItemRangeRemoved(int positionStart, int itemCount) { | |
checkIfEmpty(); | |
} | |
}; | |
public BBWRecyclerView(Context context) { | |
super(context); | |
} | |
public BBWRecyclerView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public BBWRecyclerView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
private void checkIfEmpty() { | |
if (emptyView != null && getAdapter() != null) { | |
final boolean emptyViewVisible = getAdapter().getItemCount() == 0; | |
emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE); | |
setVisibility(emptyViewVisible ? GONE : VISIBLE); | |
} | |
} | |
@Override | |
public void setAdapter(Adapter adapter) { | |
final Adapter oldAdapter = getAdapter(); | |
if (oldAdapter != null) { | |
oldAdapter.unregisterAdapterDataObserver(observer); | |
} | |
super.setAdapter(adapter); | |
if (adapter != null) { | |
adapter.registerAdapterDataObserver(observer); | |
} | |
checkIfEmpty(); | |
} | |
public void setEmptyView(View emptyView) { | |
this.emptyView = emptyView; | |
checkIfEmpty(); | |
} | |
public boolean isEmptyViewShown() { | |
return emptyView != null && emptyView.getVisibility() == View.VISIBLE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment