|
| 1 | +--- |
| 2 | +id: basics-component-scrollview |
| 3 | +title: ScrollView |
| 4 | +layout: docs |
| 5 | +category: Basics |
| 6 | +permalink: docs/basics-component-scrollview.html |
| 7 | +next: basics-component-listview |
| 8 | +--- |
| 9 | + |
| 10 | +Given the screen sizes of mobile devices, the ability to scroll through data is generally paramount for a proper usability experience. |
| 11 | + |
| 12 | +The [`ScrollView`](/react-native/docs/scrollview.html) is a generic scrolling container that can host multiple components and views. The scrollable items need not be homogenous, and you can scroll both vertically and horizontally (by setting the `horizontal` property). |
| 13 | + |
| 14 | +`ScrollView` works best to present a list of short, static items of a known quantity. All the elements and views of a `ScrollView` are rendered a priori, even if they are not currently shown on the screen. Contrast this with a `ListView`, which render only those views that are on the screen and remove views that go off-screen. |
| 15 | + |
| 16 | +> [`TextView`](/react-native/docs/basics-component-textview.html) and [`ListView`](/react-native/docs/basics-component-listview.html) are specialized scrollable containers. |
| 17 | +
|
| 18 | +This contrived example creates a horizontal `ScrollView` with a static amount of heterogenous elements (images and text). |
| 19 | + |
| 20 | +```JavaScript |
| 21 | +import React, { AppRegistry, ScrollView, Image, Text, View } from 'react-native' |
| 22 | + |
| 23 | +var SimpleScrollView = React.createClass({ |
| 24 | + render(){ |
| 25 | + return( |
| 26 | + <ScrollView horizontal={true}> |
| 27 | + <View> |
| 28 | + <Image source={require('./img/check.png')} /> |
| 29 | + </View> |
| 30 | + <View> |
| 31 | + <Image source={require('./img/check.png')} /> |
| 32 | + </View> |
| 33 | + <View> |
| 34 | + <Image source={require('./img/check.png')} /> |
| 35 | + </View> |
| 36 | + <View> |
| 37 | + <Text style={{fontSize:96}}>Text1</Text> |
| 38 | + </View> |
| 39 | + <View> |
| 40 | + <Text style={{fontSize:96}}>Text2</Text> |
| 41 | + </View> |
| 42 | + <View> |
| 43 | + <Text style={{fontSize:96}}>Text3</Text> |
| 44 | + </View> |
| 45 | + <View> |
| 46 | + <Text style={{fontSize:96}}>Text4</Text> |
| 47 | + </View> |
| 48 | + <View> |
| 49 | + <Image source={require('./img/check.png')} /> |
| 50 | + </View> |
| 51 | + <View> |
| 52 | + <Image source={require('./img/check.png')} /> |
| 53 | + </View> |
| 54 | + <View> |
| 55 | + <Image source={require('./img/check.png')} /> |
| 56 | + </View> |
| 57 | + <View> |
| 58 | + <Text style={{fontSize:96}}>Text5</Text> |
| 59 | + </View> |
| 60 | + <View> |
| 61 | + <Text style={{fontSize:96}}>Text6</Text> |
| 62 | + </View> |
| 63 | + </ScrollView> |
| 64 | + ); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | + |
| 69 | +AppRegistry.registerComponent('MyApp', () => SimpleScrollView); |
| 70 | +``` |
0 commit comments