SnapHelper
abstract class SnapHelper : RecyclerView.OnFlingListener
| kotlin.Any | ||
| ↳ | androidx.recyclerview.widget.RecyclerView.OnFlingListener | |
| ↳ | androidx.recyclerview.widget.SnapHelper |
LinearSnapHelper |
Implementation of the |
PagerSnapHelper |
Implementation of the |
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 and handle fling manually.
Summary
Public constructors |
|---|
Public functions |
|
|---|---|
Unit |
attachToRecyclerView(recyclerView: RecyclerView?)Attaches the |
abstract IntArray<Int>? |
calculateDistanceToFinalSnap(Override this method to snap to a particular point within the target view or the container view on any axis. |
IntArray<Int>! |
calculateScrollDistance(velocityX: Int, velocityY: Int)Calculated the estimated scroll distance in each direction given velocities on both axes. |
abstract View? |
findSnapView(layoutManager: RecyclerView.LayoutManager!)Override this method to provide a particular target view for snapping. |
abstract Int |
findTargetSnapPosition(Override to provide a particular adapter target position for snapping. |
Boolean |
Override this to handle a fling given the velocities in both x and y directions. |
Protected functions |
|
|---|---|
RecyclerView.SmoothScroller? |
createScroller(layoutManager: RecyclerView.LayoutManager)Creates a scroller to be used in the snapping implementation. |
LinearSmoothScroller? |
This function is deprecated. use |
Public constructors
Public functions
attachToRecyclerView
fun attachToRecyclerView(recyclerView: RecyclerView?): Unit
Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener. 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 |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if there is already a |
java.lang.IllegalStateException |
calculateDistanceToFinalSnap
abstract 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 |
targetView: View |
the target view that is chosen as the view to snap |
calculateScrollDistance
fun calculateScrollDistance(velocityX: Int, velocityY: Int): IntArray<Int>!
Calculated the estimated scroll distance in each direction given velocities on both axes.
findSnapView
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 |
| Returns | |
|---|---|
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 |
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 |
onFling
fun onFling(velocityX: Int, velocityY: Int): Boolean
Override this to handle a fling given the velocities in both x and y directions. Note that this method will only be called if the associated LayoutManager supports scrolling and the fling is not handled by nested scrolls first.
| Parameters | |
|---|---|
velocityX: Int |
the fling velocity on the X axis |
velocityY: Int |
the fling velocity on the Y axis |
| Returns | |
|---|---|
Boolean |
true if the fling was handled, false otherwise. |
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 |
| Returns | |
|---|---|
RecyclerView.SmoothScroller? |
a |
protected funcreateSnapScroller(layoutManager: RecyclerView.LayoutManager): LinearSmoothScroller?
Creates a scroller to be used in the snapping implementation.
| Parameters | |
|---|---|
layoutManager: RecyclerView.LayoutManager |
The |
| Returns | |
|---|---|
LinearSmoothScroller? |
a |