public class LinearSmoothScroller extends RecyclerView.SmoothScroller
RecyclerView.SmoothScroller
implementation which uses a LinearInterpolator
until
the target position becomes a child of the RecyclerView and then uses a
DecelerateInterpolator
to slowly approach to target position.
If the RecyclerView.LayoutManager
you are using does not implement the
RecyclerView.SmoothScroller.ScrollVectorProvider
interface, then you must override the
computeScrollVectorForPosition(int)
method. All the LayoutManagers bundled with
the support library implement this interface.
RecyclerView.SmoothScroller.Action, RecyclerView.SmoothScroller.ScrollVectorProvider
Modifier and Type | Field and Description |
---|---|
protected DecelerateInterpolator |
mDecelerateInterpolator |
protected int |
mInterimTargetDx |
protected int |
mInterimTargetDy |
protected LinearInterpolator |
mLinearInterpolator |
protected PointF |
mTargetVector |
static int |
SNAP_TO_ANY
Decides if the child should be snapped from start or end, depending on where it
currently is in relation to its parent.
|
static int |
SNAP_TO_END
Align child view's right or bottom with parent view's right or bottom
|
static int |
SNAP_TO_START
Align child view's left or top with parent view's left or top
|
Constructor and Description |
---|
LinearSmoothScroller(Context context) |
Modifier and Type | Method and Description |
---|---|
int |
calculateDtToFit(int viewStart,
int viewEnd,
int boxStart,
int boxEnd,
int snapPreference)
|
int |
calculateDxToMakeVisible(View view,
int snapPreference)
Calculates the horizontal scroll amount necessary to make the given view fully visible
inside the RecyclerView.
|
int |
calculateDyToMakeVisible(View view,
int snapPreference)
Calculates the vertical scroll amount necessary to make the given view fully visible
inside the RecyclerView.
|
protected float |
calculateSpeedPerPixel(DisplayMetrics displayMetrics)
Calculates the scroll speed.
|
protected int |
calculateTimeForDeceleration(int dx)
Calculates the time for deceleration so that transition from LinearInterpolator to
DecelerateInterpolator looks smooth.
|
protected int |
calculateTimeForScrolling(int dx)
Calculates the time it should take to scroll the given distance (in pixels)
|
PointF |
computeScrollVectorForPosition(int targetPosition)
Compute the scroll vector for a given target position.
|
protected int |
getHorizontalSnapPreference()
When scrolling towards a child view, this method defines whether we should align the left
or the right edge of the child with the parent RecyclerView.
|
protected int |
getVerticalSnapPreference()
When scrolling towards a child view, this method defines whether we should align the top
or the bottom edge of the child with the parent RecyclerView.
|
protected void |
onSeekTargetStep(int dx,
int dy,
RecyclerView.State state,
RecyclerView.SmoothScroller.Action action)
RecyclerView will call this method each time it scrolls until it can find the target
position in the layout.
|
protected void |
onStart()
Called when smooth scroll is started.
|
protected void |
onStop()
Called when smooth scroller is stopped.
|
protected void |
onTargetFound(View targetView,
RecyclerView.State state,
RecyclerView.SmoothScroller.Action action)
Called when the target position is laid out.
|
protected void |
updateActionForInterimTarget(RecyclerView.SmoothScroller.Action action)
When the target scroll position is not a child of the RecyclerView, this method calculates
a direction vector towards that child and triggers a smooth scroll.
|
findViewByPosition, getChildCount, getChildPosition, getLayoutManager, getTargetPosition, instantScrollToPosition, isPendingInitialRun, isRunning, normalize, onChildAttachedToWindow, setTargetPosition, stop
public static final int SNAP_TO_START
public static final int SNAP_TO_END
public static final int SNAP_TO_ANY
Decides if the child should be snapped from start or end, depending on where it currently is in relation to its parent.
For instance, if the view is virtually on the left of RecyclerView, using
SNAP_TO_ANY
is the same as using SNAP_TO_START
protected final LinearInterpolator mLinearInterpolator
protected final DecelerateInterpolator mDecelerateInterpolator
protected PointF mTargetVector
protected int mInterimTargetDx
protected int mInterimTargetDy
public LinearSmoothScroller(Context context)
protected void onStart()
onStart
in class RecyclerView.SmoothScroller
protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action)
RecyclerView.SmoothScroller.Action
to define the scroll
details towards the target view.onTargetFound
in class RecyclerView.SmoothScroller
targetView
- The view element which render the target position.state
- Transient state of RecyclerViewaction
- Action instance that you should update to define final scroll action
towards the targetViewprotected void onSeekTargetStep(int dx, int dy, RecyclerView.State state, RecyclerView.SmoothScroller.Action action)
RecyclerView will call this method each time it scrolls until it can find the target position in the layout.
SmoothScroller should check dx, dy and if scroll should be changed, update the
provided RecyclerView.SmoothScroller.Action
to define the next scroll.
onSeekTargetStep
in class RecyclerView.SmoothScroller
dx
- Last scroll amount horizontallydy
- Last scroll amount verticallystate
- Transient state of RecyclerViewaction
- If you want to trigger a new smooth scroll and cancel the previous one,
update this object.protected void onStop()
onStop
in class RecyclerView.SmoothScroller
RecyclerView.SmoothScroller.stop()
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
displayMetrics
- DisplayMetrics to be used for real dimension calculationsprotected int calculateTimeForDeceleration(int dx)
Calculates the time for deceleration so that transition from LinearInterpolator to DecelerateInterpolator looks smooth.
dx
- Distance to scrollprotected int calculateTimeForScrolling(int dx)
dx
- Distance in pixels that we want to scrollcalculateSpeedPerPixel(android.util.DisplayMetrics)
protected int getHorizontalSnapPreference()
SNAP_TO_START
,
SNAP_TO_END
,
SNAP_TO_ANY
protected int getVerticalSnapPreference()
SNAP_TO_START
,
SNAP_TO_END
,
SNAP_TO_ANY
protected void updateActionForInterimTarget(RecyclerView.SmoothScroller.Action action)
computeScrollVectorForPosition(int)
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference)
public int calculateDyToMakeVisible(View view, int snapPreference)
view
- The view which we want to make fully visiblesnapPreference
- The edge which the view should snap to when entering the visible
area. One of SNAP_TO_START
, SNAP_TO_END
or
SNAP_TO_ANY
.public int calculateDxToMakeVisible(View view, int snapPreference)
view
- The view which we want to make fully visiblesnapPreference
- The edge which the view should snap to when entering the visible
area. One of SNAP_TO_START
, SNAP_TO_END
or
SNAP_TO_END
@Nullable public PointF computeScrollVectorForPosition(int targetPosition)
This method can return null if the layout manager cannot calculate a scroll vector for the given position (e.g. it has no current scroll position).
targetPosition
- the position to which the scroller is scrolling