Skip to content

Instantly share code, notes, and snippets.

@mzgreen
Created February 28, 2015 08:36
Show Gist options
  • Save mzgreen/09ef2f24440fbef5f17a to your computer and use it in GitHub Desktop.
Save mzgreen/09ef2f24440fbef5f17a to your computer and use it in GitHub Desktop.
HidingScrollListener class with fixed top position hiding
public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
private static final float HIDE_THRESHOLD = 10;
private static final float SHOW_THRESHOLD = 70;
private int mToolbarOffset = 0;
private boolean mControlsVisible = true;
private int mToolbarHeight;
private int mTotalScrolledDistance;
public HidingScrollListener(Context context) {
mToolbarHeight = Utils.getToolbarHeight(context);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
if(mTotalScrolledDistance < mToolbarHeight) {
setVisible();
} else {
if (mControlsVisible) {
if (mToolbarOffset > HIDE_THRESHOLD) {
setInvisible();
} else {
setVisible();
}
} else {
if ((mToolbarHeight - mToolbarOffset) > SHOW_THRESHOLD) {
setVisible();
} else {
setInvisible();
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//...
mTotalScrolledDistance += dy;
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment