LinearSmoothScroller


class LinearSmoothScroller : 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 method. All the LayoutManagers bundled with the support library implement this interface.

Summary

Constants

const Int

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

const Int

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

const Int

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

Public constructors

Public functions

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

Helper method for calculateDxToMakeVisible and calculateDyToMakeVisible

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

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

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

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

Protected functions

Float

Calculates the scroll speed.

Int

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

Int

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

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.

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.

Unit
onSeekTargetStep(
    dx: Int,
    dy: Int,
    state: RecyclerView.State!,
    action: RecyclerView.SmoothScroller.Action!
)

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

Unit

Called when smooth scroll is started.

Unit

Called when smooth scroller is stopped.

Unit
onTargetFound(
    targetView: View!,
    state: RecyclerView.State!,
    action: RecyclerView.SmoothScroller.Action!
)

Called when the target position is laid out.

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

From androidx.recyclerview.widget.RecyclerView.SmoothScroller
PointF?

Compute the scroll vector for a given target position.

View!
Int
Int
RecyclerView.LayoutManager?
Int

Returns the adapter position of the target item

Unit

This function is deprecated.

Use jumpTo.

Boolean

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

Boolean
Unit
normalize(scrollVector: PointF)

Normalizes the vector.

Unit
Unit
setTargetPosition(targetPosition: Int)
Unit

Stops running the SmoothScroller in each animation callback.

Constants

SNAP_TO_ANY

Added in 1.0.0
const val SNAP_TO_ANY = 0: 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

SNAP_TO_END

Added in 1.0.0
const val SNAP_TO_END = 1: Int

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

SNAP_TO_START

Added in 1.0.0
const val SNAP_TO_START = -1: Int

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

Public constructors

LinearSmoothScroller

Added in 1.0.0
LinearSmoothScroller(context: Context!)

Public functions

calculateDtToFit

Added in 1.0.0
fun calculateDtToFit(
    viewStart: Int,
    viewEnd: Int,
    boxStart: Int,
    boxEnd: Int,
    snapPreference: Int
): Int

Helper method for calculateDxToMakeVisible and calculateDyToMakeVisible

calculateDxToMakeVisible

Added in 1.0.0
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: Int

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

Returns
Int

The vertical scroll amount necessary to make the view visible with the given snap preference.

calculateDyToMakeVisible

Added in 1.0.0
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: Int

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.

Returns
Int

The vertical scroll amount necessary to make the view visible with the given snap preference.

Protected functions

calculateSpeedPerPixel

Added in 1.0.0
protected 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

Returns
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

Added in 1.0.0
protected 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

Returns
Int

Time for DecelerateInterpolator to smoothly traverse the distance when transitioning from LinearInterpolation

calculateTimeForScrolling

Added in 1.0.0
protected 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

Returns
Int

Time in milliseconds

getHorizontalSnapPreference

Added in 1.0.0
protected 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.

Returns
Int

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

getVerticalSnapPreference

Added in 1.0.0
protected 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.

Returns
Int

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

onSeekTargetStep

Added in 1.4.0-rc01
protected 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 to define the next scroll.

onStart

Added in 1.4.0-rc01
protected fun onStart(): Unit

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

onStop

Added in 1.4.0-rc01
protected fun onStop(): Unit

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

onTargetFound

Added in 1.4.0-rc01
protected 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 to define the scroll details towards the target view.

updateActionForInterimTarget

Added in 1.0.0
protected 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.

Protected properties

mDecelerateInterpolator

Added in 1.0.0
protected val mDecelerateInterpolatorDecelerateInterpolator!

mInterimTargetDx

Added in 1.0.0
protected val mInterimTargetDxInt

mInterimTargetDy

Added in 1.0.0
protected val mInterimTargetDyInt

mLinearInterpolator

Added in 1.0.0
protected val mLinearInterpolatorLinearInterpolator!

mTargetVector

Added in 1.0.0
protected val mTargetVectorPointF!