Skip to content

Instantly share code, notes, and snippets.

@konk3r
Last active March 12, 2016 03:05
Show Gist options
  • Save konk3r/dd01afcacec51b15ab92 to your computer and use it in GitHub Desktop.
Save konk3r/dd01afcacec51b15ab92 to your computer and use it in GitHub Desktop.
public class RecyclerViewEmptySupport extends RecyclerView {
@Inject ViewFadeAnimator viewFadeAnimator;
private int marginTop;
private View progressView;
private View emptyView;
public RecyclerViewEmptySupport(Context context) {
this(context, null);
}
public RecyclerViewEmptySupport(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RecyclerViewEmptySupport(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inject();
}
private void inject() {
PathfinderApp app = (PathfinderApp) getContext().getApplicationContext();
ObjectGraph appGraph = Injector.obtain(app);
appGraph.inject(this);
}
private TransactionListener transactionListener = new TransactionListener() {
@Override
public void startTransaction() {
}
@Override
public void finishTransaction(boolean didChangesOccur) {
if (!didChangesOccur) {
return;
}
setViewVisibility();
}
};
private void setViewVisibility() {
Adapter<?> adapter = getAdapter();
if(adapter != null && progressView != null) {
if(adapter.getItemCount() == 0 && getVisibility() == VISIBLE) {
viewFadeAnimator.fadeOutView(progressView);
viewFadeAnimator.fadeOutView(RecyclerViewEmptySupport.this);
viewFadeAnimator.fadeInView(emptyView);
}
else if (getVisibility() != VISIBLE) {
viewFadeAnimator.fadeOutView(progressView);
viewFadeAnimator.fadeOutView(emptyView);
viewFadeAnimator.fadeInView(RecyclerViewEmptySupport.this);
}
}
}
public void displayProgressView() {
progressView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
RecyclerViewEmptySupport.this.setVisibility(View.GONE);
}
public void setAdapter(BaseAdapter adapter) {
super.setAdapter(adapter);
if(adapter != null) {
adapter.registerTransactionListener(transactionListener);
}
}
public void setProgressView(View progressView) {
this.progressView = progressView;
}
public void setEmptyView(View emptyView) {
this.emptyView = emptyView;
}
public void setBaseTopMargin(int margin) {
this.marginTop = margin;
}
public void setEmptyContentOffset(int offset) {
setMarginOffset(offset, progressView);
setMarginOffset(offset, emptyView);
}
private void setMarginOffset(int offset, View view) {
if (view == null) {
return;
}
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
if (params.topMargin == marginTop + offset) {
return;
}
params.setMargins(0, marginTop + offset, 0, 0);
view.setLayoutParams(params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment