224 lines
5.9 KiB
JavaScript
224 lines
5.9 KiB
JavaScript
|
/**
|
||
|
* The examples provided by Facebook are for non-commercial testing and
|
||
|
* evaluation purposes only.
|
||
|
*
|
||
|
* Facebook reserves all rights not expressly granted.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
||
|
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
*
|
||
|
*/
|
||
|
'use strict';
|
||
|
|
||
|
var React = require('react-native');
|
||
|
var {
|
||
|
Image,
|
||
|
StyleSheet,
|
||
|
Text,
|
||
|
TouchableWithoutFeedback,
|
||
|
TouchableOpacity,
|
||
|
View,
|
||
|
ViewPagerAndroid,
|
||
|
} = React;
|
||
|
|
||
|
var PAGES = 5;
|
||
|
var BGCOLOR = ['#fdc08e', '#fff6b9', '#99d1b7', '#dde5fe', '#f79273'];
|
||
|
var IMAGE_URIS = [
|
||
|
'http://apod.nasa.gov/apod/image/1410/20141008tleBaldridge001h990.jpg',
|
||
|
'http://apod.nasa.gov/apod/image/1409/volcanicpillar_vetter_960.jpg',
|
||
|
'http://apod.nasa.gov/apod/image/1409/m27_snyder_960.jpg',
|
||
|
'http://apod.nasa.gov/apod/image/1409/PupAmulti_rot0.jpg',
|
||
|
'http://apod.nasa.gov/apod/image/1510/lunareclipse_27Sep_beletskycrop4.jpg',
|
||
|
];
|
||
|
|
||
|
var LikeCount = React.createClass({
|
||
|
getInitialState: function() {
|
||
|
return {
|
||
|
likes: 7,
|
||
|
};
|
||
|
},
|
||
|
onClick: function() {
|
||
|
this.setState({likes: this.state.likes + 1});
|
||
|
},
|
||
|
render: function() {
|
||
|
var thumbsUp = '\uD83D\uDC4D';
|
||
|
return (
|
||
|
<View style={styles.likeContainer}>
|
||
|
<TouchableOpacity onPress={this.onClick} style={styles.likeButton}>
|
||
|
<Text style={styles.likesText}>
|
||
|
{thumbsUp + ' Like'}
|
||
|
</Text>
|
||
|
</TouchableOpacity>
|
||
|
<Text style={styles.likesText}>
|
||
|
{this.state.likes + ' likes'}
|
||
|
</Text>
|
||
|
</View>
|
||
|
);
|
||
|
},
|
||
|
});
|
||
|
|
||
|
var Button = React.createClass({
|
||
|
_handlePress: function() {
|
||
|
if (this.props.enabled && this.props.onPress) {
|
||
|
this.props.onPress();
|
||
|
}
|
||
|
},
|
||
|
render: function() {
|
||
|
return (
|
||
|
<TouchableWithoutFeedback onPress={this._handlePress}>
|
||
|
<View style={[styles.button, this.props.enabled ? {} : styles.buttonDisabled]}>
|
||
|
<Text style={styles.buttonText}>{this.props.text}</Text>
|
||
|
</View>
|
||
|
</TouchableWithoutFeedback>
|
||
|
);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var ProgressBar = React.createClass({
|
||
|
render: function() {
|
||
|
var fractionalPosition = (this.props.progress.position + this.props.progress.offset);
|
||
|
var progressBarSize = (fractionalPosition / (PAGES - 1)) * this.props.size;
|
||
|
return (
|
||
|
<View style={[styles.progressBarContainer, {width: this.props.size}]}>
|
||
|
<View style={[styles.progressBar, {width: progressBarSize}]}/>
|
||
|
</View>
|
||
|
);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var ViewPagerAndroidExample = React.createClass({
|
||
|
statics: {
|
||
|
title: '<ViewPagerAndroid>',
|
||
|
description: 'Container that allows to flip left and right between child views.'
|
||
|
},
|
||
|
getInitialState: function() {
|
||
|
return {page: 0, progress: {position: 0, offset: 0}};
|
||
|
},
|
||
|
onPageSelected: function(e) {
|
||
|
this.setState({page: e.nativeEvent.position});
|
||
|
},
|
||
|
onPageScroll: function(e) {
|
||
|
this.setState({progress: e.nativeEvent});
|
||
|
},
|
||
|
move: function(delta) {
|
||
|
var page = this.state.page + delta;
|
||
|
this.viewPager && this.viewPager.setPage(page);
|
||
|
this.setState({page});
|
||
|
},
|
||
|
go: function(page) {
|
||
|
this.viewPager && this.viewPager.setPage(page);
|
||
|
this.setState({page});
|
||
|
},
|
||
|
render: function() {
|
||
|
var pages = [];
|
||
|
for (var i = 0; i < PAGES; i++) {
|
||
|
var pageStyle = {
|
||
|
backgroundColor: BGCOLOR[i % BGCOLOR.length],
|
||
|
alignItems: 'center',
|
||
|
padding: 20,
|
||
|
};
|
||
|
pages.push(
|
||
|
<View key={i} style={pageStyle} collapsable={false}>
|
||
|
<Image
|
||
|
style={styles.image}
|
||
|
source={{uri: IMAGE_URIS[i % BGCOLOR.length]}}
|
||
|
/>
|
||
|
<LikeCount />
|
||
|
</View>
|
||
|
);
|
||
|
}
|
||
|
var page = this.state.page;
|
||
|
return (
|
||
|
<View style={styles.container}>
|
||
|
<ViewPagerAndroid
|
||
|
style={styles.viewPager}
|
||
|
initialPage={0}
|
||
|
onPageScroll={this.onPageScroll}
|
||
|
onPageSelected={this.onPageSelected}
|
||
|
ref={viewPager => { this.viewPager = viewPager; }}>
|
||
|
{pages}
|
||
|
</ViewPagerAndroid>
|
||
|
<View style={styles.buttons}>
|
||
|
<Button text="Start" enabled={page > 0} onPress={() => this.go(0)}/>
|
||
|
<Button text="Prev" enabled={page > 0} onPress={() => this.move(-1)}/>
|
||
|
<Text style={styles.buttonText}>Page {page + 1} / {PAGES}</Text>
|
||
|
<ProgressBar size={100} progress={this.state.progress}/>
|
||
|
<Button text="Next" enabled={page < PAGES - 1} onPress={() => this.move(1)}/>
|
||
|
<Button text="Last" enabled={page < PAGES - 1} onPress={() => this.go(PAGES - 1)}/>
|
||
|
</View>
|
||
|
</View>
|
||
|
);
|
||
|
},
|
||
|
});
|
||
|
|
||
|
var styles = StyleSheet.create({
|
||
|
buttons: {
|
||
|
flexDirection: 'row',
|
||
|
height: 30,
|
||
|
backgroundColor: 'black',
|
||
|
alignItems: 'center',
|
||
|
justifyContent: 'space-between',
|
||
|
},
|
||
|
button: {
|
||
|
flex: 1,
|
||
|
width: 0,
|
||
|
margin: 5,
|
||
|
borderColor: 'gray',
|
||
|
borderWidth: 1,
|
||
|
backgroundColor: 'gray',
|
||
|
},
|
||
|
buttonDisabled: {
|
||
|
backgroundColor: 'black',
|
||
|
opacity: 0.5,
|
||
|
},
|
||
|
buttonText: {
|
||
|
color: 'white',
|
||
|
},
|
||
|
container: {
|
||
|
flex: 1,
|
||
|
backgroundColor: 'white',
|
||
|
},
|
||
|
image: {
|
||
|
width: 300,
|
||
|
height: 200,
|
||
|
padding: 20,
|
||
|
},
|
||
|
likeButton: {
|
||
|
backgroundColor: 'rgba(0, 0, 0, 0.1)',
|
||
|
borderColor: '#333333',
|
||
|
borderWidth: 1,
|
||
|
borderRadius: 5,
|
||
|
flex: 1,
|
||
|
margin: 8,
|
||
|
padding: 8,
|
||
|
},
|
||
|
likeContainer: {
|
||
|
flexDirection: 'row',
|
||
|
},
|
||
|
likesText: {
|
||
|
flex: 1,
|
||
|
fontSize: 18,
|
||
|
alignSelf: 'center',
|
||
|
},
|
||
|
progressBarContainer: {
|
||
|
height: 10,
|
||
|
margin: 10,
|
||
|
borderColor: '#eeeeee',
|
||
|
borderWidth: 2,
|
||
|
},
|
||
|
progressBar: {
|
||
|
alignSelf: 'flex-start',
|
||
|
flex: 1,
|
||
|
backgroundColor: '#eeeeee',
|
||
|
},
|
||
|
viewPager: {
|
||
|
flex: 1,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
module.exports = ViewPagerAndroidExample;
|