RecyclerView.SmoothScroller

public abstract class RecyclerView.SmoothScroller

Known direct subclasses
LinearSmoothScroller

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.


Base class for smooth scrolling. Handles basic tracking of the target view position and provides methods to trigger a programmatic scroll.

An instance of SmoothScroller is only intended to be used once. You should create a new instance for each call to startSmoothScroll.

Summary

Nested types

Holds information about a smooth scroll request by a SmoothScroller.

An interface which is optionally implemented by custom RecyclerView.LayoutManager to provide a hint to a SmoothScroller about the location of the target position.

Public constructors

Public methods

@Nullable PointF
computeScrollVectorForPosition(int targetPosition)

Compute the scroll vector for a given target position.

View
findViewByPosition(int position)
int
int
@Nullable RecyclerView.LayoutManager
int

Returns the adapter position of the target item

void

This method is deprecated.

Use jumpTo.

boolean

Returns true if SmoothScroller has been started but has not received the first animation callback yet.

boolean
void
setTargetPosition(int targetPosition)

Protected methods

void
normalize(@NonNull PointF scrollVector)

Normalizes the vector.

void
abstract void
onSeekTargetStep(
    @Px int dx,
    @Px int dy,
    @NonNull RecyclerView.State state,
    @NonNull RecyclerView.SmoothScroller.Action action
)

RecyclerView will call this method each time it scrolls until it can find the target position in the layout.

abstract void

Called when smooth scroll is started.

abstract void

Called when smooth scroller is stopped.

abstract void
onTargetFound(
    @NonNull View targetView,
    @NonNull RecyclerView.State state,
    @NonNull RecyclerView.SmoothScroller.Action action
)

Called when the target position is laid out.

final void

Stops running the SmoothScroller in each animation callback.

Public constructors

SmoothScroller

Added in 1.0.0
public SmoothScroller()

Public methods

computeScrollVectorForPosition

Added in 1.0.0
public @Nullable PointF computeScrollVectorForPosition(int targetPosition)

Compute the scroll vector for a given target position.

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).

Parameters
int targetPosition

the position to which the scroller is scrolling

Returns
@Nullable PointF

the scroll vector for a given target position

findViewByPosition

Added in 1.0.0
public View findViewByPosition(int position)

getChildCount

Added in 1.0.0
public int getChildCount()
See also
getChildCount

getChildPosition

Added in 1.0.0
public int getChildPosition(View view)

getLayoutManager

Added in 1.0.0
public @Nullable RecyclerView.LayoutManager getLayoutManager()
Returns
@Nullable RecyclerView.LayoutManager

The LayoutManager to which this SmoothScroller is attached. Will return null after the SmoothScroller is stopped.

getTargetPosition

Added in 1.0.0
public int getTargetPosition()

Returns the adapter position of the target item

Returns
int

Adapter position of the target item or NO_POSITION if no target view is set.

instantScrollToPosition

Added in 1.0.0
Deprecated in 1.0.0
public void instantScrollToPosition(int position)
See also
scrollToPosition

isPendingInitialRun

Added in 1.0.0
public boolean isPendingInitialRun()

Returns true if SmoothScroller has been started but has not received the first animation callback yet.

Returns
boolean

True if this SmoothScroller is waiting to start

isRunning

Added in 1.0.0
public boolean isRunning()
Returns
boolean

True if SmoothScroller is currently active

setTargetPosition

Added in 1.0.0
public void setTargetPosition(int targetPosition)

Protected methods

normalize

Added in 1.0.0
protected void normalize(@NonNull PointF scrollVector)

Normalizes the vector.

Parameters
@NonNull PointF scrollVector

The vector that points to the target scroll position

onChildAttachedToWindow

Added in 1.0.0
protected void onChildAttachedToWindow(View child)

onSeekTargetStep

Added in 1.0.0
protected abstract void onSeekTargetStep(
    @Px int dx,
    @Px int dy,
    @NonNull RecyclerView.State state,
    @NonNull 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 Action to define the next scroll.

Parameters
@Px int dx

Last scroll amount horizontally

@Px int dy

Last scroll amount vertically

@NonNull RecyclerView.State state

Transient state of RecyclerView

@NonNull RecyclerView.SmoothScroller.Action action

If you want to trigger a new smooth scroll and cancel the previous one, update this object.

onStart

Added in 1.0.0
protected abstract void onStart()

Called when smooth scroll is started. This might be a good time to do setup.

onStop

Added in 1.0.0
protected abstract void onStop()

Called when smooth scroller is stopped. This is a good place to cleanup your state etc.

See also
stop

onTargetFound

Added in 1.0.0
protected abstract void onTargetFound(
    @NonNull View targetView,
    @NonNull RecyclerView.State state,
    @NonNull RecyclerView.SmoothScroller.Action action
)

Called when the target position is laid out. This is the last callback SmoothScroller will receive and it should update the provided Action to define the scroll details towards the target view.

Parameters
@NonNull View targetView

The view element which render the target position.

@NonNull RecyclerView.State state

Transient state of RecyclerView

@NonNull RecyclerView.SmoothScroller.Action action

Action instance that you should update to define final scroll action towards the targetView

stop

Added in 1.0.0
protected final void stop()

Stops running the SmoothScroller in each animation callback. Note that this does not cancel any existing Action updated by onTargetFound or onSeekTargetStep.