Created
December 6, 2017 02:55
-
-
Save chiemy/430738838a011a385af880f1193a75b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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