PagerSnapHelper


class PagerSnapHelper : SnapHelper


Implementation of the SnapHelper supporting pager style snapping in either vertical or horizontal orientation.

PagerSnapHelper can help achieve a similar behavior to androidx.viewpager.widget.ViewPager. Set both RecyclerView and the items of the RecyclerView.Adapter to have MATCH_PARENT height and width and then attach PagerSnapHelper to the RecyclerView using attachToRecyclerView.

Summary

Public constructors

Public functions

IntArray<Int>?
calculateDistanceToFinalSnap(
    layoutManager: RecyclerView.LayoutManager,
    targetView: View
)

Override this method to snap to a particular point within the target view or the container view on any axis.

View?

Override this method to provide a particular target view for snapping.

Int
findTargetSnapPosition(
    layoutManager: RecyclerView.LayoutManager!,
    velocityX: Int,
    velocityY: Int
)

Override to provide a particular adapter target position for snapping.

Protected functions

RecyclerView.SmoothScroller?

Creates a scroller to be used in the snapping implementation.

Inherited functions

From androidx.recyclerview.widget.SnapHelper
Unit

Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener.

IntArray<Int>!
calculateScrollDistance(velocityX: Int, velocityY: Int)

Calculated the estimated scroll distance in each direction given velocities on both axes.

LinearSmoothScroller?

This function is deprecated.

use createScroller instead.

Boolean
onFling(velocityX: Int, velocityY: Int)

Override this to handle a fling given the velocities in both x and y directions.

Public constructors

PagerSnapHelper

Added in 1.0.0
PagerSnapHelper()

Public functions

calculateDistanceToFinalSnap

Added in 1.4.0-rc01
fun calculateDistanceToFinalSnap(
    layoutManager: RecyclerView.LayoutManager,
    targetView: View
): IntArray<Int>?

Override this method to snap to a particular point within the target view or the container view on any axis.

This method is called when the SnapHelper has intercepted a fling and it needs to know the exact distance required to scroll by in order to snap to the target view.

Parameters
layoutManager: RecyclerView.LayoutManager

the RecyclerView.LayoutManager associated with the attached RecyclerView

targetView: View

the target view that is chosen as the view to snap

Returns
IntArray<Int>?

the output coordinates the put the result into. out[0] is the distance on horizontal axis and out[1] is the distance on vertical axis.

findSnapView

Added in 1.4.0-rc01
fun findSnapView(layoutManager: RecyclerView.LayoutManager!): View?

Override this method to provide a particular target view for snapping.

This method is called when the SnapHelper is ready to start snapping and requires a target view to snap to. It will be explicitly called when the scroll state becomes idle after a scroll. It will also be called when the SnapHelper is preparing to snap after a fling and requires a reference view from the current set of child views.

If this method returns null, SnapHelper will not snap to any view.

Parameters
layoutManager: RecyclerView.LayoutManager!

the RecyclerView.LayoutManager associated with the attached RecyclerView

Returns
View?

the target view to which to snap on fling or end of scroll

findTargetSnapPosition

Added in 1.4.0-rc01
fun findTargetSnapPosition(
    layoutManager: RecyclerView.LayoutManager!,
    velocityX: Int,
    velocityY: Int
): Int

Override to provide a particular adapter target position for snapping.

Parameters
layoutManager: RecyclerView.LayoutManager!

the RecyclerView.LayoutManager associated with the attached RecyclerView

velocityX: Int

fling velocity on the horizontal axis

velocityY: Int

fling velocity on the vertical axis

Returns
Int

the target adapter position to you want to snap or NO_POSITION if no snapping should happen

Protected functions

createScroller

protected fun createScroller(layoutManager: RecyclerView.LayoutManager): RecyclerView.SmoothScroller?

Creates a scroller to be used in the snapping implementation.

Parameters
layoutManager: RecyclerView.LayoutManager

The RecyclerView.LayoutManager associated with the attached RecyclerView.

Returns
RecyclerView.SmoothScroller?

a RecyclerView.SmoothScroller which will handle the scrolling.