Skip to content

Instantly share code, notes, and snippets.

@chiemy
Created December 6, 2017 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiemy/430738838a011a385af880f1193a75b1 to your computer and use it in GitHub Desktop.
Save chiemy/430738838a011a385af880f1193a75b1 to your computer and use it in GitHub Desktop.
public class SnappingLinearLayoutManager extends LinearLayoutManager {
private RecyclerView.SmoothScroller smoothScroller;
public SnappingLinearLayoutManager(Context context) {
super(context, VERTICAL, false);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
if (smoothScroller == null) {
smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
}
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
private static final float MILLISECONDS_PER_INCH = 25f; //default is 25f (bigger =
// slower)
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SnappingLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment