Created
September 8, 2016 09:00
-
-
Save BenDLH/32ed21a712c91664955e46b66097ed4f 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
// | |
// Usage : | |
// recyclerView.setLayoutManager(new SmoothScrollingLayoutManager(getContext())); | |
// recyclerView.smoothScrollToPosition(0); | |
// | |
// | |
// SmoothScrollingLayoutManager | |
// Makersphere | |
// | |
// Created by Ben on 02/06/2016. | |
// Copyright (c) 2016 SHAPE A/S. All rights reserved. | |
// | |
public class SmoothScrollingLayoutManager extends LinearLayoutManager { | |
private static final float SMOOTH_SCROLL_MILLIS = 100; | |
LinearSmoothScroller smoothScroller; | |
public SmoothScrollingLayoutManager(Context context) { | |
super(context); | |
smoothScroller = new SmoothScroller(context); | |
} | |
@Override | |
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { | |
smoothScroller.setTargetPosition(position); | |
startSmoothScroll(smoothScroller); | |
} | |
private class SmoothScroller extends LinearSmoothScroller { | |
public SmoothScroller(Context context) { | |
super(context); | |
} | |
@Override | |
public PointF computeScrollVectorForPosition(int targetPosition) { | |
return SmoothScrollingLayoutManager.this.computeScrollVectorForPosition(targetPosition); | |
} | |
@Override | |
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { | |
return SMOOTH_SCROLL_MILLIS / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1000, displayMetrics); | |
} | |
@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