Skip to content

Instantly share code, notes, and snippets.

@Hazer
Forked from ozodrukh/HideExtraOnScroll.java
Last active August 29, 2015 14:10
Show Gist options
  • Save Hazer/b8c9b615e69617ad1682 to your computer and use it in GitHub Desktop.
Save Hazer/b8c9b615e69617ad1682 to your computer and use it in GitHub Desktop.
public static class HideExtraOnScroll extends RecyclerView.OnScrollListener{
final static Interpolator ACCELERATE = new AccelerateInterpolator();
final static Interpolator DECELERATE = new DecelerateInterpolator();
WeakReference<View> mTarget;
HideExtraOnScrollHelper mScrollHelper;
boolean isExtraObjectsOutside;
public HideExtraOnScroll(View target) {
int minimumFlingVelocity = ViewConfiguration.get(target.getContext())
.getScaledMinimumFlingVelocity();
mScrollHelper = new HideExtraOnScrollHelper(minimumFlingVelocity);
mTarget = new WeakReference<View>(target);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final View target = mTarget.get();
if(target == null) {
return;
}
boolean needsToHideExtraObjects = mScrollHelper.needsHideExtraObjects(dy);
if(!isVisible(target) && !needsToHideExtraObjects){
show(target);
isExtraObjectsOutside = false;
}else if(isVisible(target) && needsToHideExtraObjects){
hide(target, -target.getHeight());
isExtraObjectsOutside = true;
}
}
public boolean isVisible(View target){
return !isExtraObjectsOutside;
}
public void hide(final View target, float distance){
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationY",
ViewHelper.getTranslationY(target), distance);
animator.setAutoCancel(true);
animator.setInterpolator(DECELERATE);
animator.start();
}
public void show(final View target){
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationY",
ViewHelper.getTranslationY(target), 0f);
animator.setAutoCancel(true);
animator.setInterpolator(ACCELERATE);
animator.start();
}
}
public static class HideExtraOnScrollHelper{
public final static int UNKNOWN = -1;
public final static int TOP = 0;
public final static int BOTTOM = 1;
int mDraggedAmount;
int mOldDirection;
int mDragDirection;
final int mMinFlingDistance;
public HideExtraOnScrollHelper(int minFlingDistance) {
mOldDirection =
mDragDirection =
mDraggedAmount = UNKNOWN;
mMinFlingDistance = minFlingDistance;
}
/**
* Checks need to hide extra objects on scroll or not
*
* @param dy scrolled distance y
* @return true if need to hide extra objects on screen
*/
public boolean needsHideExtraObjects(int dy){
boolean needHide = false;
mDragDirection = dy > 0 ? BOTTOM : TOP;
if(mDragDirection != mOldDirection){
mDraggedAmount = 0;
}
mDraggedAmount += dy;
if(mDragDirection == TOP && Math.abs(mDraggedAmount) > mMinFlingDistance){
needHide = false;
}else if(mDragDirection == BOTTOM && mDraggedAmount > mMinFlingDistance){
needHide = true;
}
if(mOldDirection != mDragDirection){
mOldDirection = mDragDirection;
}
return needHide;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment