concept ViewPagerAndroid in category react native

appears as: ViewPagerAndroid
React Native in Action

This is an excerpt from Manning's book React Native in Action.

11.3 Implementing scrollable paging with ViewPagerAndroid

Next, you’ll create a new example page and component using ViewPagerAndroid. This component allows you to easily swipe left and right between views. Every child of ViewPagerAndroid is treated as its own separate, swipeable view (see figure 11.3).

Figure 11.3 ViewPagerAndroid with two child views. When you swipe the pages, they scroll left and right to show the next page.

c11_03.tif

To get started, create an app/ViewPager.js file and add the code in listing 11.7 to implement the ViewPagerAndroid component.

Listing 11.7 Using ViewPagerAndroid to enable a scrollable paging view
import React, { Component } from 'react'
import {
  ViewPagerAndroid,    #1  
  View,
  Text
} from 'react-native'

let styles

class ViewPager extends Component {
  render () {
    const {
      pageStyle,
      page1Style,
      page2Style,
      textStyle
    } = styles
    return (
      <ViewPagerAndroid    #2  
        style={{ flex: 1 }}
        initialPage={0}>
        <View style={[ pageStyle, page1Style ]}>
          <Text style={textStyle}>First page</Text>
        </View>
        <View style={[ pageStyle, page2Style ]}>
          <Text style={textStyle}>Second page</Text>
        </View>
      </ViewPagerAndroid>
    )
  }
}

styles = {
  pageStyle: {
    justifyContent: 'center',
    alignItems: 'center',
    padding: 20,
    flex: 1,
  },
  page1Style: {
    backgroundColor: 'orange'
  },
  page2Style: {
    backgroundColor: 'red'
  },
  textStyle: {
    fontSize: 18,
    color: 'white'
  }
}

export default ViewPager

#1   Imports ViewPagerAndroid from React Native
#2   Returns ViewPagerAndroid with two child views, one with an orange background and one with a red background