# PagerSnapHelper

Artifact: [androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview) [View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/PagerSnapHelper.java+class:androidx.recyclerview.widget.PagerSnapHelper) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***

<br />

```
class PagerSnapHelper : SnapHelper
```

<br />

|---|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) ||||
| ↳ | [androidx.recyclerview.widget.RecyclerView.OnFlingListener](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.OnFlingListener) |||
|   | ↳ | [androidx.recyclerview.widget.SnapHelper](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper) ||
|   |   | ↳ | [androidx.recyclerview.widget.PagerSnapHelper](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/PagerSnapHelper) |

*** ** * ** ***

Implementation of the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper` supporting pager style snapping in either vertical or horizontal orientation.

PagerSnapHelper can help achieve a similar behavior to `https://developer.android.com/reference/kotlin/androidx/viewpager/widget/ViewPager`. Set both `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` and the items of the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter` to have `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT` height and width and then attach PagerSnapHelper to the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` using `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper#attachToRecyclerView(androidx.recyclerview.widget.RecyclerView)`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/PagerSnapHelper#PagerSnapHelper()()` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html>?` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/PagerSnapHelper#calculateDistanceToFinalSnap(androidx.recyclerview.widget.RecyclerView.LayoutManager,android.view.View)( layoutManager: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager, targetView: https://developer.android.com/reference/android/view/View.html )` Override this method to snap to a particular point within the target view or the container view on any axis. |
| `https://developer.android.com/reference/android/view/View.html?` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/PagerSnapHelper#findSnapView(androidx.recyclerview.widget.RecyclerView.LayoutManager)(layoutManager: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager!)` Override this method to provide a particular target view for snapping. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/PagerSnapHelper#findTargetSnapPosition(androidx.recyclerview.widget.RecyclerView.LayoutManager,int,int)( layoutManager: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager!, velocityX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, velocityY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Override to provide a particular adapter target position for snapping. |

| ### Protected functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.SmoothScroller?` | `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/PagerSnapHelper#createScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager)(layoutManager: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager)` Creates a scroller to be used in the snapping implementation. |

### Inherited functions

From [androidx.recyclerview.widget.SnapHelper](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper#attachToRecyclerView(androidx.recyclerview.widget.RecyclerView)(recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView?)`

Attaches the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper` to the provided RecyclerView, by calling `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView#setOnFlingListener(androidx.recyclerview.widget.RecyclerView.OnFlingListener)`.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html>!` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper#calculateScrollDistance(int,int)(velocityX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, velocityY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Calculated the estimated scroll distance in each direction given velocities on both axes.
`https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearSmoothScroller?` `[createSnapScroller](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper#createSnapScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager))(layoutManager: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager)`

**This function is deprecated.**

use `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper#createScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager)` instead.

<br />

`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/SnapHelper#onFling(int,int)(velocityX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, velocityY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

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](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.4.0)

```
fun calculateDistanceToFinalSnap(
    layoutManager: RecyclerView.LayoutManager,
    targetView: View
): Array<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 `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/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: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` | the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` associated with the attached `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` |
| `targetView: https://developer.android.com/reference/android/view/View.html` | the target view that is chosen as the view to snap |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html>?` | 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](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.4.0)

```
fun findSnapView(layoutManager: RecyclerView.LayoutManager!): View?
```

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

This method is called when the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/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 `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/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: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager!` | the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` associated with the attached `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/View.html?` | the target view to which to snap on fling or end of scroll |

### findTargetSnapPosition

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.4.0)

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

Override to provide a particular adapter target position for snapping.

| Parameters |
|---|---|
| `layoutManager: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager!` | the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` associated with the attached `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` |
| `velocityX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | fling velocity on the horizontal axis |
| `velocityY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | fling velocity on the vertical axis |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the target adapter position to you want to snap or `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView#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: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` | The `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` associated with the attached `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.SmoothScroller?` | a `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.SmoothScroller` which will handle the scrolling. |