Register now for Android Dev Summit 2019!

SnapHelper

abstract class SnapHelper : RecyclerView.OnFlingListener
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.OnFlingListener
   ↳ androidx.recyclerview.widget.SnapHelper

Class intended to support snapping for a RecyclerView.

SnapHelper tries to handle fling as well but for this to work properly, the RecyclerView.LayoutManager must implement the RecyclerView.SmoothScroller.ScrollVectorProvider interface or you should override onFling(int, int) and handle fling manually.

Summary

Public constructors

Class intended to support snapping for a RecyclerView.

Public methods

open Unit
attachToRecyclerView(@Nullable recyclerView: RecyclerView?)

Attaches the SnapHelper to the provided RecyclerView, by calling RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener).

abstract IntArray?
calculateDistanceToFinalSnap(@NonNull layoutManager: RecyclerView.LayoutManager, @NonNull targetView: View)

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

open IntArray!
calculateScrollDistance(velocityX: Int, velocityY: Int)

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

abstract View?

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

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

Override to provide a particular adapter target position for snapping.

open Boolean
onFling(velocityX: Int, velocityY: Int)

Protected methods

open RecyclerView.SmoothScroller?

Creates a scroller to be used in the snapping implementation.

open LinearSmoothScroller?

Creates a scroller to be used in the snapping implementation.

Public constructors

<init>

SnapHelper()

Class intended to support snapping for a RecyclerView.

SnapHelper tries to handle fling as well but for this to work properly, the RecyclerView.LayoutManager must implement the RecyclerView.SmoothScroller.ScrollVectorProvider interface or you should override onFling(int, int) and handle fling manually.

Public methods

attachToRecyclerView

open fun attachToRecyclerView(@Nullable recyclerView: RecyclerView?): Unit

Attaches the SnapHelper to the provided RecyclerView, by calling RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener). You can call this method with null to detach it from the current RecyclerView.

Parameters
recyclerView RecyclerView?: The RecyclerView instance to which you want to add this helper or null if you want to remove SnapHelper from the current RecyclerView.
Exceptions
IllegalArgumentException if there is already a RecyclerView.OnFlingListener attached to the provided RecyclerView.

calculateDistanceToFinalSnap

@Nullable abstract fun calculateDistanceToFinalSnap(@NonNull layoutManager: RecyclerView.LayoutManager, @NonNull targetView: View): IntArray?

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 RecyclerView.LayoutManager: the target view that is chosen as the view to snap
Return
IntArray?: 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.

calculateScrollDistance

open fun calculateScrollDistance(velocityX: Int, velocityY: Int): IntArray!

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

Parameters
velocityX Int: Fling velocity on the horizontal axis.
velocityY Int: Fling velocity on the vertical axis.
Return
IntArray!: array holding the calculated distances in x and y directions respectively.

findSnapView

@Nullable abstract 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
Return
View?: the target view to which to snap on fling or end of scroll

findTargetSnapPosition

abstract 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 RecyclerView.LayoutManager!: fling velocity on the horizontal axis
velocityY RecyclerView.LayoutManager!: fling velocity on the vertical axis
Return
Int: the target adapter position to you want to snap or RecyclerView#NO_POSITION if no snapping should happen

onFling

open fun onFling(velocityX: Int, velocityY: Int): Boolean

Protected methods

createScroller

@Nullable protected open 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.
Return
RecyclerView.SmoothScroller?: a RecyclerView.SmoothScroller which will handle the scrolling.

createSnapScroller

@Nullable protected open fun createSnapScroller(layoutManager: RecyclerView.LayoutManager!): LinearSmoothScroller?

Deprecated: use createScroller(RecyclerView.LayoutManager) instead.

Creates a scroller to be used in the snapping implementation.

Parameters
layoutManager RecyclerView.LayoutManager!: The RecyclerView.LayoutManager associated with the attached RecyclerView.
Return
LinearSmoothScroller?: a LinearSmoothScroller which will handle the scrolling.