2017-11-08 00:34:50 +00:00
---
id: viewpagerandroid
title: ViewPagerAndroid
layout: docs
category: components
permalink: docs/viewpagerandroid.html
next: virtualizedlist
previous: view
---
Container that allows to flip left and right between child views. Each
child view of the `ViewPagerAndroid` will be treated as a separate page
and will be stretched to fill the `ViewPagerAndroid` .
It is important all children are `<View>` s and not composite components.
You can set style properties like `padding` or `backgroundColor` for each
child. It is also important that each child have a `key` prop.
Example:
```
render: function() {
return (
< ViewPagerAndroid
style={styles.viewPager}
initialPage={0}>
< View style = {styles.pageStyle} key = "1" >
< Text > First page< / Text >
< / View >
< View style = {styles.pageStyle} key = "2" >
< Text > Second page< / Text >
< / View >
< / ViewPagerAndroid >
);
}
...
var styles = {
...
viewPager: {
flex: 1
},
pageStyle: {
alignItems: 'center',
padding: 20,
}
}
```
### Props
Migrate to new documentation format
Summary:
Now that the Component and API docs are no longer auto-generated, we need to consolidate on a new format for our jsdoc comments. Any help from the community will be appreciated.
In this initial pull request, we'll be tackling the following docs:
- `AccessibilityInfo`, an API doc.
- `ActivityIndicator`, a Component doc.
- `View`, a Component doc.
This top comment will serve as a style guide, and when in doubt, please refer to the individual commits in this PR.
Each commit should update a single component or API, along with any relevant markdown files.
- Documentation in the JavaScript source files should be succinct. Any verbosity should be moved over to the markdown docs in the website...
- ...by adding a link to the relevant method/prop on the website to every comment block.
- Avoid markdown style links in JavaScript source files, opt for plain old URIs.
Let code document itself:
- If a method is Flow typed, the comment block does not need to repeat this information.
- If a param can be one of several values, and the type definition is easily determined from the code, the values should not be repeated in the comment block. Again, move this to the markdown doc if not present already.
Closes https://github.com/facebook/react-native/pull/16790
Differential Revision: D6353840
Pulled By: hramos
fbshipit-source-id: 9712c459acc33092aae9909f3dd0b58a00b26afc
2017-11-17 00:44:29 +00:00
- [View props... ](docs/view.html#props )
2017-11-08 00:34:50 +00:00
- [`initialPage` ](docs/viewpagerandroid.html#initialpage )
- [`keyboardDismissMode` ](docs/viewpagerandroid.html#keyboarddismissmode )
- [`onPageScroll` ](docs/viewpagerandroid.html#onpagescroll )
- [`onPageScrollStateChanged` ](docs/viewpagerandroid.html#onpagescrollstatechanged )
- [`onPageSelected` ](docs/viewpagerandroid.html#onpageselected )
- [`pageMargin` ](docs/viewpagerandroid.html#pagemargin )
- [`peekEnabled` ](docs/viewpagerandroid.html#peekenabled )
- [`scrollEnabled` ](docs/viewpagerandroid.html#scrollenabled )
### Type Definitions
- [`ViewPagerScrollState` ](docs/viewpagerandroid.html#viewpagerscrollstate )
---
# Reference
## Props
### `initialPage`
Index of initial page that should be selected. Use `setPage` method to
update the page, and `onPageSelected` to monitor page changes
| Type | Required |
| - | - |
| number | No |
---
### `keyboardDismissMode`
Determines whether the keyboard gets dismissed in response to a drag.
2017-11-09 17:47:50 +00:00
- 'none' (the default), drags do not dismiss the keyboard.
- 'on-drag', the keyboard is dismissed when a drag begins.
2017-11-08 00:34:50 +00:00
| Type | Required |
| - | - |
| enum('none', 'on-drag') | No |
---
### `onPageScroll`
2017-11-09 17:47:50 +00:00
Executed when transitioning between pages (ether because of animation for the requested page change or when user is swiping/dragging between pages) The `event.nativeEvent` object for this callback will carry following data:
- position - index of first page from the left that is currently visible
- offset - value from range [0,1) describing stage between page transitions.
Value x means that (1 - x) fraction of the page at "position" index is visible, and x fraction of the next page is visible.
2017-11-08 00:34:50 +00:00
| Type | Required |
| - | - |
| function | No |
---
### `onPageScrollStateChanged`
Function called when the page scrolling state has changed.
The page scrolling state can be in 3 states:
- idle, meaning there is no interaction with the page scroller happening at the time
- dragging, meaning there is currently an interaction with the page scroller
- settling, meaning that there was an interaction with the page scroller, and the
page scroller is now finishing it's closing or opening animation
| Type | Required |
| - | - |
| function | No |
---
### `onPageSelected`
This callback will be called once ViewPager finish navigating to selected page
(when user swipes between pages). The `event.nativeEvent` object passed to this
callback will have following fields:
- position - index of page that has been selected
| Type | Required |
| - | - |
| function | No |
---
### `pageMargin`
Blank space to show between pages. This is only visible while scrolling, pages are still
edge-to-edge.
| Type | Required |
| - | - |
| number | No |
---
### `peekEnabled`
Whether enable showing peekFraction or not. If this is true, the preview of
last and next page will show in current screen. Defaults to false.
| Type | Required |
| - | - |
| bool | No |
---
### `scrollEnabled`
When false, the content does not scroll.
The default value is true.
| Type | Required |
| - | - |
| bool | No |
## Type Definitions
### ViewPagerScrollState
| Type |
| - |
| $Enum |
**Constants:**
| Value | Description |
| - | - |
| idle | |
| dragging | |
| settling | |