# LinearSnapHelper

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

*** ** * ** ***


```
public class LinearSnapHelper extends SnapHelper
```

<br />

|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||||
| ↳ | [androidx.recyclerview.widget.RecyclerView.OnFlingListener](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.OnFlingListener) |||
|   | ↳ | [androidx.recyclerview.widget.SnapHelper](https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper) ||
|   |   | ↳ | [androidx.recyclerview.widget.LinearSnapHelper](https://developer.android.com/reference/androidx/recyclerview/widget/LinearSnapHelper) |

*** ** * ** ***

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

The implementation will snap the center of the target child view to the center of the attached `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView`. If you intend to change this behavior then override `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#calculateDistanceToFinalSnap(androidx.recyclerview.widget.RecyclerView.LayoutManager,android.view.View)`.

## Summary

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

| ### Public methods |
|---|---|
| `int[]` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSnapHelper#calculateDistanceToFinalSnap(androidx.recyclerview.widget.RecyclerView.LayoutManager,android.view.View)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html targetView )` 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/androidx/recyclerview/widget/LinearSnapHelper#findSnapView(androidx.recyclerview.widget.RecyclerView.LayoutManager)(https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager)` Override this method to provide a particular target view for snapping. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSnapHelper#findTargetSnapPosition(androidx.recyclerview.widget.RecyclerView.LayoutManager,int,int)( https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY )` Override to provide a particular adapter target position for snapping. |

| ### Inherited methods |
|---|
| From [androidx.recyclerview.widget.SnapHelper](https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper) |---|---| | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#attachToRecyclerView(androidx.recyclerview.widget.RecyclerView)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView recyclerView)` Attaches the `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper` to the provided RecyclerView, by calling `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#setOnFlingListener(androidx.recyclerview.widget.RecyclerView.OnFlingListener)`. | | `int[]` | `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#calculateScrollDistance(int,int)(int velocityX, int velocityY)` Calculated the estimated scroll distance in each direction given velocities on both axes. | | `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller` | `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#createScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager)` Creates a scroller to be used in the snapping implementation. | | `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller` | `[createSnapScroller](https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#createSnapScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager)` **This method is deprecated.** use `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#createScroller(androidx.recyclerview.widget.RecyclerView.LayoutManager)` instead. <br /> | | `boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/SnapHelper#onFling(int,int)(int velocityX, int velocityY)` Override this to handle a fling given the velocities in both x and y directions. | |

## Public constructors

### LinearSnapHelper

Added in 1.0.0

```
public LinearSnapHelper()
```

## Public methods

### calculateDistanceToFinalSnap

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

```
public int[] calculateDistanceToFinalSnap(
    @NonNull RecyclerView.LayoutManager layoutManager,
    @NonNull View targetView
)
```

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/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager` | the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager` associated with the attached `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html targetView` | the target view that is chosen as the view to snap |

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

```
public View findSnapView(RecyclerView.LayoutManager layoutManager)
```

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

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

```
public int findTargetSnapPosition(
    RecyclerView.LayoutManager layoutManager,
    int velocityX,
    int velocityY
)
```

Override to provide a particular adapter target position for snapping.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager layoutManager` | the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager` associated with the attached `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView` |
| `int velocityX` | fling velocity on the horizontal axis |
| `int velocityY` | fling velocity on the vertical axis |

| Returns |
|---|---|
| `int` | the target adapter position to you want to snap or `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_POSITION()` if no snapping should happen |