LinearSmoothScroller

open class LinearSmoothScroller : RecyclerView.SmoothScroller
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.SmoothScroller
   ↳ androidx.recyclerview.widget.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.

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.

Summary

Constants

static Int

Decides if the child should be snapped from start or end, depending on where it currently is in relation to its parent.

static Int

Align child view's right or bottom with parent view's right or bottom

static Int

Align child view's left or top with parent view's left or top

Public constructors

<init>(context: Context!)

Public methods

open Int
calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int)

Helper method for calculateDxToMakeVisible(android.view.View, int) and calculateDyToMakeVisible(android.view.View, int)

open Int
calculateDxToMakeVisible(view: View!, snapPreference: Int)

Calculates the horizontal scroll amount necessary to make the given view fully visible inside the RecyclerView.

open Int
calculateDyToMakeVisible(view: View!, snapPreference: Int)

Calculates the vertical scroll amount necessary to make the given view fully visible inside the RecyclerView.

Protected methods

open Float

Calculates the scroll speed.

open Int

Calculates the time for deceleration so that transition from LinearInterpolator to DecelerateInterpolator looks smooth.

open Int

Calculates the time it should take to scroll the given distance (in pixels)

open Int

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.

open Int

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.

open Unit

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

open Unit

Called when smooth scroll is started.

open Unit

Called when smooth scroller is stopped.

open Unit

Called when the target position is laid out.

open Unit

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.

Inherited functions

Properties

DecelerateInterpolator!

Int

Int

LinearInterpolator!

PointF!

Constants

SNAP_TO_ANY

static val SNAP_TO_ANY: Int

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

Value: 0

SNAP_TO_END

static val SNAP_TO_END: Int

Align child view's right or bottom with parent view's right or bottom

Value: 1

SNAP_TO_START

static val SNAP_TO_START: Int

Align child view's left or top with parent view's left or top

Value: -1

Public constructors

<init>

LinearSmoothScroller(context: Context!)

Public methods

calculateDtToFit

open fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int

Helper method for calculateDxToMakeVisible(android.view.View, int) and calculateDyToMakeVisible(android.view.View, int)

calculateDxToMakeVisible

open fun calculateDxToMakeVisible(view: View!, snapPreference: Int): Int

Calculates the horizontal scroll amount necessary to make the given view fully visible inside the RecyclerView.

Parameters
view View!: The view which we want to make fully visible
snapPreference View!: 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
Return
Int: The vertical scroll amount necessary to make the view visible with the given snap preference.

calculateDyToMakeVisible

open fun calculateDyToMakeVisible(view: View!, snapPreference: Int): Int

Calculates the vertical scroll amount necessary to make the given view fully visible inside the RecyclerView.

Parameters
view View!: The view which we want to make fully visible
snapPreference View!: 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.
Return
Int: The vertical scroll amount necessary to make the view visible with the given snap preference.

Protected methods

calculateSpeedPerPixel

protected open fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics!): Float

Calculates the scroll speed.

By default, LinearSmoothScroller assumes this method always returns the same value and caches the result of calling it.

Parameters
displayMetrics DisplayMetrics!: DisplayMetrics to be used for real dimension calculations
Return
Float: The time (in ms) it should take for each pixel. For instance, if returned value is 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds.

calculateTimeForDeceleration

protected open fun calculateTimeForDeceleration(dx: Int): Int

Calculates the time for deceleration so that transition from LinearInterpolator to DecelerateInterpolator looks smooth.

Parameters
dx Int: Distance to scroll
Return
Int: Time for DecelerateInterpolator to smoothly traverse the distance when transitioning from LinearInterpolation

calculateTimeForScrolling

protected open fun calculateTimeForScrolling(dx: Int): Int

Calculates the time it should take to scroll the given distance (in pixels)

Parameters
dx Int: Distance in pixels that we want to scroll
Return
Int: Time in milliseconds

getHorizontalSnapPreference

protected open fun getHorizontalSnapPreference(): Int

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.

Return
Int: SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector

getVerticalSnapPreference

protected open fun getVerticalSnapPreference(): Int

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.

Return
Int: SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector

onSeekTargetStep

protected open fun onSeekTargetStep(dx: Int, dy: Int, state: RecyclerView.State, 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.

onStart

protected open fun onStart(): Unit

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

onStop

protected open fun onStop(): Unit

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

onTargetFound

protected open fun onTargetFound(targetView: View, state: RecyclerView.State, 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

updateActionForInterimTarget

protected open fun updateActionForInterimTarget(action: RecyclerView.SmoothScroller.Action!): Unit

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.

Properties

mDecelerateInterpolator

protected val mDecelerateInterpolator: DecelerateInterpolator!

mInterimTargetDx

protected var mInterimTargetDx: Int

mInterimTargetDy

protected var mInterimTargetDy: Int

mLinearInterpolator

protected val mLinearInterpolator: LinearInterpolator!

mTargetVector

protected var mTargetVector: PointF!