# ViewPager2.OnPageChangeCallback

# ViewPager2.OnPageChangeCallback

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2.OnPageChangeCallback "View this page in Java")

<br />

```
abstract class ViewPager2.OnPageChangeCallback
```

<br />

*** ** * ** ***

Callback interface for responding to changing state of the selected page.

## Summary

|                                                                   ### Public constructors                                                                    |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [OnPageChangeCallback](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2.OnPageChangeCallback#OnPageChangeCallback())`()` |

|                             ### Public functions                              |
|-------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onPageScrollStateChanged](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2.OnPageChangeCallback#onPageScrollStateChanged(int))`(state: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Called when the scroll state changes.                                                                                                                                                                                                                                                                                                                                                                                                                    |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onPageScrolled](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2.OnPageChangeCallback#onPageScrolled(int,float,int))`(` ` position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` positionOffset: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`,` ` positionOffsetPixels: @`[Px](https://developer.android.com/reference/kotlin/androidx/annotation/Px)` `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` This method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onPageSelected](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2.OnPageChangeCallback#onPageSelected(int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` This method will be invoked when a new page becomes selected.                                                                                                                                                                                                                                                                                                                                                                                                             |

## Public constructors

### OnPageChangeCallback

Added in 1.0.0  

```
OnPageChangeCallback()
```  

## Public functions

### onPageScrollStateChanged

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

```
fun onPageScrollStateChanged(state: Int): Unit
```

Called when the scroll state changes. Useful for discovering when the user begins dragging, when a fake drag is started, when the pager is automatically settling to the current page, or when it is fully stopped/idle.`state`can be one of[SCROLL_STATE_IDLE](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2#SCROLL_STATE_IDLE()),[SCROLL_STATE_DRAGGING](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2#SCROLL_STATE_DRAGGING())or[SCROLL_STATE_SETTLING](https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2#SCROLL_STATE_SETTLING()).  

### onPageScrolled

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

```
fun onPageScrolled(
    position: Int,
    positionOffset: Float,
    positionOffsetPixels: @Px Int
): Unit
```

This method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll.  

|                                                                                     Parameters                                                                                     |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| `position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                            | Position index of the first page currently being displayed. Page position+1 will be visible if positionOffset is nonzero. |
| `positionOffset: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)                                                                                  | Value from \[0, 1) indicating the offset from the page at position.                                                       |
| `positionOffsetPixels: @`[Px](https://developer.android.com/reference/kotlin/androidx/annotation/Px)` `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Value in pixels indicating the offset from position.                                                                      |

### onPageSelected

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

```
fun onPageSelected(position: Int): Unit
```

This method will be invoked when a new page becomes selected. Animation is not necessarily complete.  

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|------------------------------------------|
| `position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position index of the new selected page. |