Skip to content

Instantly share code, notes, and snippets.

@Anrimian
Created February 25, 2018 11:48
Show Gist options
  • Select an option

  • Save Anrimian/7c60d9063d955cd6f1beadfa4512e51e to your computer and use it in GitHub Desktop.

Select an option

Save Anrimian/7c60d9063d955cd6f1beadfa4512e51e to your computer and use it in GitHub Desktop.
public class HideFabScrollListener extends RecyclerView.OnScrollListener {
private static final int TRANSLATE_HIDE_DURATION_MILLIS = 200;
private static final int TRANSLATE_SHOW_DURATION_MILLIS = 170;
private View fab;
private Animator hideAnimator;
private Animator showAnimator;
public HideFabScrollListener(View fab) {
this.fab = fab;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0 && hideAnimator == null) {
if (showAnimator != null ) {
showAnimator.cancel();
}
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
float translation = fab.getHeight() + params.bottomMargin;
hideAnimator = ObjectAnimator.ofFloat(fab, "translationY", fab.getTranslationY(), translation);
hideAnimator.setDuration(TRANSLATE_HIDE_DURATION_MILLIS);
hideAnimator.setInterpolator(new AccelerateInterpolator());
hideAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
hideAnimator = null;
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
hideAnimator = null;
}
});
hideAnimator.start();
} else if (dy < 0 && showAnimator == null) {
if (hideAnimator != null) {
hideAnimator.cancel();
}
showAnimator = ObjectAnimator.ofFloat(fab, "translationY", fab.getTranslationY(), 0);
showAnimator.setDuration(TRANSLATE_SHOW_DURATION_MILLIS);
showAnimator.setInterpolator(new DecelerateInterpolator());
showAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
showAnimator = null;
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
showAnimator = null;
}
});
showAnimator.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment