SmoothScroller

abstract class SmoothScroller
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.SmoothScroller

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 LayoutManager#startSmoothScroll(SmoothScroller).

Summary

Nested classes
open

Holds information about a smooth scroll request by a SmoothScroller.

abstract

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
open Boolean

open View!

open Int

open RecyclerView.LayoutManager?

open Int

Returns the adapter position of the target item

open Unit

open Unit
setTargetPosition(targetPosition: Int)

open Int

open PointF?

Compute the scroll vector for a given target position.

open Boolean

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

Protected methods
open Unit
normalize(@NonNull scrollVector: PointF)

Normalizes the vector.

abstract Unit
onSeekTargetStep(@Px dx: Int, @Px dy: Int, @NonNull state: RecyclerView.State, @NonNull action: RecyclerView.SmoothScroller.Action)

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

abstract Unit

Called when smooth scroller is stopped.

Unit

Stops running the SmoothScroller in each animation callback.

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

Called when the target position is laid out.

open Unit

abstract Unit

Called when smooth scroll is started.

Public constructors

<init>

SmoothScroller()

Public methods

isRunning

open fun isRunning(): Boolean
Return
Boolean: True if SmoothScroller is currently active

findViewByPosition

open fun findViewByPosition(position: Int): View!

getChildPosition

open fun getChildPosition(view: View!): Int

getLayoutManager

@Nullable open fun getLayoutManager(): RecyclerView.LayoutManager?
Return
RecyclerView.LayoutManager?: The LayoutManager to which this SmoothScroller is attached. Will return null after the SmoothScroller is stopped.

getTargetPosition

open fun getTargetPosition(): Int

Returns the adapter position of the target item

Return
Int: Adapter position of the target item or RecyclerView#NO_POSITION if no target view is set.

instantScrollToPosition

open fun instantScrollToPosition(position: Int): Unit

setTargetPosition

open fun setTargetPosition(targetPosition: Int): Unit

getChildCount

open fun getChildCount(): Int

computeScrollVectorForPosition

@Nullable open fun computeScrollVectorForPosition(targetPosition: Int): PointF?

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
targetPosition Int: the position to which the scroller is scrolling
Return
PointF?: the scroll vector for a given target position

isPendingInitialRun

open fun isPendingInitialRun(): Boolean

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

Return
Boolean: True if this SmoothScroller is waiting to start

Protected methods

normalize

protected open fun normalize(@NonNull scrollVector: PointF): Unit

Normalizes the vector.

Parameters
scrollVector PointF: The vector that points to the target scroll position

onSeekTargetStep

protected abstract fun onSeekTargetStep(@Px dx: Int, @Px dy: Int, @NonNull state: RecyclerView.State, @NonNull action: RecyclerView.SmoothScroller.Action): Unit

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
dx Int: Last scroll amount horizontally
dy Int: Last scroll amount vertically
state Int: Transient state of RecyclerView
action Int: If you want to trigger a new smooth scroll and cancel the previous one, update this object.

onStop

protected abstract fun onStop(): Unit

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

See Also

stop

protected fun stop(): Unit

Stops running the SmoothScroller in each animation callback. Note that this does not cancel any existing Action updated by onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action) or onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action).

onTargetFound

protected abstract fun onTargetFound(@NonNull targetView: View, @NonNull state: RecyclerView.State, @NonNull action: RecyclerView.SmoothScroller.Action): Unit

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
targetView View: The view element which render the target position.
state View: Transient state of RecyclerView
action View: Action instance that you should update to define final scroll action towards the targetView

onChildAttachedToWindow

protected open fun onChildAttachedToWindow(child: View!): Unit

onStart

protected abstract fun onStart(): Unit

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