# LinearSmoothScroller

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

*** ** * ** ***


```
public class LinearSmoothScroller extends RecyclerView.SmoothScroller
```

<br />

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

*** ** * ** ***

`https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller` implementation which uses a `https://developer.android.com/reference/android/view/animation/LinearInterpolator.html` until the target position becomes a child of the RecyclerView and then uses a `https://developer.android.com/reference/android/view/animation/DecelerateInterpolator.html` to slowly approach to target position.

If the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager` you are using does not implement the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller.ScrollVectorProvider` interface, then you must override the `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#computeScrollVectorForPosition(int)` method. All the LayoutManagers bundled with the support library implement this interface.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_ANY() = 0` Decides if the child should be snapped from start or end, depending on where it currently is in relation to its parent. |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_END() = 1` Align child view's right or bottom with parent view's right or bottom |
| `static final int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_START() = -1` Align child view's left or top with parent view's left or top |

| ### Protected fields |
|---|---|
| `final https://developer.android.com/reference/android/view/animation/DecelerateInterpolator.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#mDecelerateInterpolator()` |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#mInterimTargetDx()` |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#mInterimTargetDy()` |
| `final https://developer.android.com/reference/android/view/animation/LinearInterpolator.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#mLinearInterpolator()` |
| `https://developer.android.com/reference/android/graphics/PointF.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#mTargetVector()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#LinearSmoothScroller(android.content.Context)(https://developer.android.com/reference/android/content/Context.html context)` |

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDtToFit(int,int,int,int,int)( int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference )` Helper method for `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDxToMakeVisible(android.view.View,int)` and `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDyToMakeVisible(android.view.View,int)` |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDxToMakeVisible(android.view.View,int)(https://developer.android.com/reference/android/view/View.html view, int snapPreference)` Calculates the horizontal scroll amount necessary to make the given view fully visible inside the RecyclerView. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDyToMakeVisible(android.view.View,int)(https://developer.android.com/reference/android/view/View.html view, int snapPreference)` Calculates the vertical scroll amount necessary to make the given view fully visible inside the RecyclerView. |

| ### Protected methods |
|---|---|
| `float` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateSpeedPerPixel(android.util.DisplayMetrics)(https://developer.android.com/reference/android/util/DisplayMetrics.html displayMetrics)` Calculates the scroll speed. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateTimeForDeceleration(int)(int dx)` Calculates the time for deceleration so that transition from LinearInterpolator to DecelerateInterpolator looks smooth. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateTimeForScrolling(int)(int dx)` Calculates the time it should take to scroll the given distance (in pixels) |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#getHorizontalSnapPreference()()` When scrolling towards a child view, this method defines whether we should align the left or the right edge of the child with the parent RecyclerView. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#getVerticalSnapPreference()()` When scrolling towards a child view, this method defines whether we should align the top or the bottom edge of the child with the parent RecyclerView. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#onSeekTargetStep(int,int,androidx.recyclerview.widget.RecyclerView.State,androidx.recyclerview.widget.RecyclerView.SmoothScroller.Action)( int dx, int dy, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller.Action action )` RecyclerView will call this method each time it scrolls until it can find the target position in the layout. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#onStart()()` Called when smooth scroll is started. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#onStop()()` Called when smooth scroller is stopped. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#onTargetFound(android.view.View,androidx.recyclerview.widget.RecyclerView.State,androidx.recyclerview.widget.RecyclerView.SmoothScroller.Action)( https://developer.android.com/reference/android/view/View.html targetView, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.State state, https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller.Action action )` Called when the target position is laid out. |
| `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#updateActionForInterimTarget(androidx.recyclerview.widget.RecyclerView.SmoothScroller.Action)( https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller.Action action )` When the target scroll position is not a child of the RecyclerView, this method calculates a direction vector towards that child and triggers a smooth scroll. |

| ### Inherited methods |
|---|
| From [androidx.recyclerview.widget.RecyclerView.SmoothScroller](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller) |---|---| | `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/graphics/PointF.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#computeScrollVectorForPosition(int)(int targetPosition)` Compute the scroll vector for a given target position. | | `https://developer.android.com/reference/android/view/View.html` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#findViewByPosition(int)(int position)` | | `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#getChildCount()()` | | `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#getChildPosition(android.view.View)(https://developer.android.com/reference/android/view/View.html view)` | | `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#getLayoutManager()()` | | `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#getTargetPosition()()` Returns the adapter position of the target item | | `void` | `[instantScrollToPosition](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#instantScrollToPosition(int))(int position)` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller.Action#jumpTo(int)`. <br /> | | `boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#isPendingInitialRun()()` Returns true if SmoothScroller has been started but has not received the first animation callback yet. | | `boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#isRunning()()` | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#normalize(android.graphics.PointF)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/PointF.html scrollVector)` Normalizes the vector. | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#onChildAttachedToWindow(android.view.View)(https://developer.android.com/reference/android/view/View.html child)` | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#setTargetPosition(int)(int targetPosition)` | | `final void` | `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#stop()()` Stops running the SmoothScroller in each animation callback. | |

## Constants

### SNAP_TO_ANY

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

```
public static final int SNAP_TO_ANY = 0
```

Decides if the child should be snapped from start or end, depending on where it currently is in relation to its parent.

For instance, if the view is virtually on the left of RecyclerView, using `SNAP_TO_ANY` is the same as using `SNAP_TO_START`

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDtToFit(int,int,int,int,int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDxToMakeVisible(android.view.View,int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDyToMakeVisible(android.view.View,int)` |   |

### SNAP_TO_END

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

```
public static final int SNAP_TO_END = 1
```

Align child view's right or bottom with parent view's right or bottom

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDtToFit(int,int,int,int,int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDxToMakeVisible(android.view.View,int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDyToMakeVisible(android.view.View,int)` |   |

### SNAP_TO_START

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

```
public static final int SNAP_TO_START = -1
```

Align child view's left or top with parent view's left or top

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDtToFit(int,int,int,int,int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDxToMakeVisible(android.view.View,int)` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDyToMakeVisible(android.view.View,int)` |   |

## Protected fields

### mDecelerateInterpolator

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

```
protected final DecelerateInterpolator mDecelerateInterpolator
```

### mInterimTargetDx

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

```
protected int mInterimTargetDx
```

### mInterimTargetDy

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

```
protected int mInterimTargetDy
```

### mLinearInterpolator

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

```
protected final LinearInterpolator mLinearInterpolator
```

### mTargetVector

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

```
protected PointF mTargetVector
```

## Public constructors

### LinearSmoothScroller

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

```
public LinearSmoothScroller(Context context)
```

## Public methods

### calculateDtToFit

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

```
public int calculateDtToFit(
    int viewStart,
    int viewEnd,
    int boxStart,
    int boxEnd,
    int snapPreference
)
```

Helper method for `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDxToMakeVisible(android.view.View,int)` and `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateDyToMakeVisible(android.view.View,int)`

### calculateDxToMakeVisible

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

```
public int calculateDxToMakeVisible(View view, int snapPreference)
```

Calculates the horizontal scroll amount necessary to make the given view fully visible inside the RecyclerView.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/view/View.html view` | The view which we want to make fully visible |
| `int snapPreference` | The edge which the view should snap to when entering the visible area. One of `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_START()`, `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_END()` or `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_END()` |

| Returns |
|---|---|
| `int` | The vertical scroll amount necessary to make the view visible with the given snap preference. |

### calculateDyToMakeVisible

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

```
public int calculateDyToMakeVisible(View view, int snapPreference)
```

Calculates the vertical scroll amount necessary to make the given view fully visible inside the RecyclerView.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/view/View.html view` | The view which we want to make fully visible |
| `int snapPreference` | The edge which the view should snap to when entering the visible area. One of `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_START()`, `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_END()` or `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_ANY()`. |

| Returns |
|---|---|
| `int` | The vertical scroll amount necessary to make the view visible with the given snap preference. |

## Protected methods

### calculateSpeedPerPixel

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

```
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
```

Calculates the scroll speed.

By default, LinearSmoothScroller assumes this method always returns the same value and caches the result of calling it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/util/DisplayMetrics.html displayMetrics` | DisplayMetrics to be used for real dimension calculations |

| Returns |
|---|---|
| `float` | The time (in ms) it should take for each pixel. For instance, if returned value is 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds. |

### calculateTimeForDeceleration

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

```
protected int calculateTimeForDeceleration(int dx)
```

Calculates the time for deceleration so that transition from LinearInterpolator to DecelerateInterpolator looks smooth.

| Parameters |
|---|---|
| `int dx` | Distance to scroll |

| Returns |
|---|---|
| `int` | Time for DecelerateInterpolator to smoothly traverse the distance when transitioning from LinearInterpolation |

### calculateTimeForScrolling

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

```
protected int calculateTimeForScrolling(int dx)
```

Calculates the time it should take to scroll the given distance (in pixels)

| Parameters |
|---|---|
| `int dx` | Distance in pixels that we want to scroll |

| Returns |
|---|---|
| `int` | Time in milliseconds |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#calculateSpeedPerPixel(android.util.DisplayMetrics)` |   |

### getHorizontalSnapPreference

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

```
protected int getHorizontalSnapPreference()
```

When scrolling towards a child view, this method defines whether we should align the left or the right edge of the child with the parent RecyclerView.

| Returns |
|---|---|
| `int` | SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_START()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_END()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_ANY()` |   |

### getVerticalSnapPreference

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

```
protected int getVerticalSnapPreference()
```

When scrolling towards a child view, this method defines whether we should align the top or the bottom edge of the child with the parent RecyclerView.

| Returns |
|---|---|
| `int` | SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_START()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_END()` |   |
| `https://developer.android.com/reference/androidx/recyclerview/widget/LinearSmoothScroller#SNAP_TO_ANY()` |   |

### onSeekTargetStep

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

```
protected void onSeekTargetStep(
    int dx,
    int dy,
    RecyclerView.State state,
    RecyclerView.SmoothScroller.Action action
)
```

RecyclerView will call this method each time it scrolls until it can find the target position in the layout.

SmoothScroller should check dx, dy and if scroll should be changed, update the provided to define the next scroll.

### onStart

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

```
protected void onStart()
```

Called when smooth scroll is started. This might be a good time to do setup.

### onStop

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

```
protected void onStop()
```

Called when smooth scroller is stopped. This is a good place to cleanup your state etc.

### onTargetFound

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

```
protected void onTargetFound(
    View targetView,
    RecyclerView.State state,
    RecyclerView.SmoothScroller.Action action
)
```

Called when the target position is laid out. This is the last callback SmoothScroller will receive and it should update the provided to define the scroll details towards the target view.

### updateActionForInterimTarget

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

```
protected void updateActionForInterimTarget(
    RecyclerView.SmoothScroller.Action action
)
```

When the target scroll position is not a child of the RecyclerView, this method calculates a direction vector towards that child and triggers a smooth scroll.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.SmoothScroller#computeScrollVectorForPosition(int)` |   |