2015-10-07 02:43:31 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-04-08 01:08:29 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-04-08 01:08:29 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-10-07 02:43:31 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2015-10-07 02:43:31 +00:00
|
|
|
var {
|
|
|
|
Image,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TouchableWithoutFeedback,
|
|
|
|
TouchableOpacity,
|
|
|
|
View,
|
|
|
|
ViewPagerAndroid,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-10-07 02:43:31 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
import type {ViewPagerScrollState} from 'ViewPagerAndroid';
|
2016-01-29 09:16:40 +00:00
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
var PAGES = 5;
|
|
|
|
var BGCOLOR = ['#fdc08e', '#fff6b9', '#99d1b7', '#dde5fe', '#f79273'];
|
|
|
|
var IMAGE_URIS = [
|
2016-11-03 15:04:42 +00:00
|
|
|
'https://apod.nasa.gov/apod/image/1410/20141008tleBaldridge001h990.jpg',
|
|
|
|
'https://apod.nasa.gov/apod/image/1409/volcanicpillar_vetter_960.jpg',
|
|
|
|
'https://apod.nasa.gov/apod/image/1409/m27_snyder_960.jpg',
|
|
|
|
'https://apod.nasa.gov/apod/image/1409/PupAmulti_rot0.jpg',
|
|
|
|
'https://apod.nasa.gov/apod/image/1510/lunareclipse_27Sep_beletskycrop4.jpg',
|
2015-10-07 02:43:31 +00:00
|
|
|
];
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class LikeCount extends React.Component {
|
|
|
|
state = {
|
|
|
|
likes: 7,
|
|
|
|
};
|
|
|
|
|
|
|
|
onClick = () => {
|
2015-10-07 02:43:31 +00:00
|
|
|
this.setState({likes: this.state.likes + 1});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2015-10-07 02:43:31 +00:00
|
|
|
var thumbsUp = '\uD83D\uDC4D';
|
|
|
|
return (
|
|
|
|
<View style={styles.likeContainer}>
|
|
|
|
<TouchableOpacity onPress={this.onClick} style={styles.likeButton}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Text style={styles.likesText}>{thumbsUp + ' Like'}</Text>
|
2015-10-07 02:43:31 +00:00
|
|
|
</TouchableOpacity>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Text style={styles.likesText}>{this.state.likes + ' likes'}</Text>
|
2015-10-07 02:43:31 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-07 02:43:31 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Button extends React.Component {
|
|
|
|
_handlePress = () => {
|
2015-10-07 02:43:31 +00:00
|
|
|
if (this.props.enabled && this.props.onPress) {
|
|
|
|
this.props.onPress();
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2015-10-07 02:43:31 +00:00
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback onPress={this._handlePress}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.button,
|
|
|
|
this.props.enabled ? {} : styles.buttonDisabled,
|
|
|
|
]}>
|
2015-10-07 02:43:31 +00:00
|
|
|
<Text style={styles.buttonText}>{this.props.text}</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-10-07 02:43:31 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class ProgressBar extends React.Component {
|
|
|
|
render() {
|
2018-05-11 20:32:37 +00:00
|
|
|
var fractionalPosition =
|
|
|
|
this.props.progress.position + this.props.progress.offset;
|
2018-06-06 12:20:40 +00:00
|
|
|
var progressBarSize = (fractionalPosition / (PAGES - 1)) * this.props.size;
|
2015-10-07 02:43:31 +00:00
|
|
|
return (
|
|
|
|
<View style={[styles.progressBarContainer, {width: this.props.size}]}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<View style={[styles.progressBar, {width: progressBarSize}]} />
|
2015-10-07 02:43:31 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ViewPagerAndroidExample extends React.Component {
|
|
|
|
static title = '<ViewPagerAndroid>';
|
2018-06-06 12:20:40 +00:00
|
|
|
static description =
|
|
|
|
'Container that allows to flip left and right between child views.';
|
2016-07-26 08:00:02 +00:00
|
|
|
|
|
|
|
state = {
|
|
|
|
page: 0,
|
|
|
|
animationsAreEnabled: true,
|
|
|
|
scrollEnabled: true,
|
|
|
|
progress: {
|
|
|
|
position: 0,
|
|
|
|
offset: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
onPageSelected = e => {
|
2015-10-07 02:43:31 +00:00
|
|
|
this.setState({page: e.nativeEvent.position});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-11-13 16:03:54 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
onPageScroll = e => {
|
2015-10-07 02:43:31 +00:00
|
|
|
this.setState({progress: e.nativeEvent});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-11-13 16:03:54 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
onPageScrollStateChanged = (state: ViewPagerScrollState) => {
|
2016-01-29 09:16:40 +00:00
|
|
|
this.setState({scrollState: state});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-29 09:16:40 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
move = delta => {
|
2015-10-07 02:43:31 +00:00
|
|
|
var page = this.state.page + delta;
|
2015-11-13 16:03:54 +00:00
|
|
|
this.go(page);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-11-13 16:03:54 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
go = page => {
|
2015-11-13 16:03:54 +00:00
|
|
|
if (this.state.animationsAreEnabled) {
|
|
|
|
this.viewPager.setPage(page);
|
|
|
|
} else {
|
|
|
|
this.viewPager.setPageWithoutAnimation(page);
|
|
|
|
}
|
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
this.setState({page});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-11-13 16:03:54 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2015-10-07 02:43:31 +00:00
|
|
|
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 />
|
2018-05-11 20:32:37 +00:00
|
|
|
</View>,
|
2015-10-07 02:43:31 +00:00
|
|
|
);
|
|
|
|
}
|
2018-05-11 20:32:37 +00:00
|
|
|
var {page, animationsAreEnabled} = this.state;
|
2015-10-07 02:43:31 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<ViewPagerAndroid
|
|
|
|
style={styles.viewPager}
|
|
|
|
initialPage={0}
|
2016-05-11 23:50:12 +00:00
|
|
|
scrollEnabled={this.state.scrollEnabled}
|
2015-10-07 02:43:31 +00:00
|
|
|
onPageScroll={this.onPageScroll}
|
|
|
|
onPageSelected={this.onPageSelected}
|
2016-01-29 09:16:40 +00:00
|
|
|
onPageScrollStateChanged={this.onPageScrollStateChanged}
|
2016-04-08 01:08:29 +00:00
|
|
|
pageMargin={10}
|
2018-05-11 20:32:37 +00:00
|
|
|
ref={viewPager => {
|
|
|
|
this.viewPager = viewPager;
|
|
|
|
}}>
|
2015-10-07 02:43:31 +00:00
|
|
|
{pages}
|
|
|
|
</ViewPagerAndroid>
|
2016-05-11 23:50:12 +00:00
|
|
|
<View style={styles.buttons}>
|
|
|
|
<Button
|
|
|
|
enabled={true}
|
2018-05-11 20:32:37 +00:00
|
|
|
text={
|
|
|
|
this.state.scrollEnabled ? 'Scroll Enabled' : 'Scroll Disabled'
|
|
|
|
}
|
|
|
|
onPress={() =>
|
|
|
|
this.setState({scrollEnabled: !this.state.scrollEnabled})
|
|
|
|
}
|
2016-05-11 23:50:12 +00:00
|
|
|
/>
|
|
|
|
</View>
|
2015-11-13 16:03:54 +00:00
|
|
|
<View style={styles.buttons}>
|
2018-05-11 20:32:37 +00:00
|
|
|
{animationsAreEnabled ? (
|
2015-11-13 16:03:54 +00:00
|
|
|
<Button
|
|
|
|
text="Turn off animations"
|
|
|
|
enabled={true}
|
|
|
|
onPress={() => this.setState({animationsAreEnabled: false})}
|
2018-05-11 20:32:37 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2015-11-13 16:03:54 +00:00
|
|
|
<Button
|
|
|
|
text="Turn animations back on"
|
|
|
|
enabled={true}
|
|
|
|
onPress={() => this.setState({animationsAreEnabled: true})}
|
2018-05-11 20:32:37 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Text style={styles.scrollStateText}>
|
|
|
|
ScrollState[ {this.state.scrollState} ]
|
|
|
|
</Text>
|
2015-11-13 16:03:54 +00:00
|
|
|
</View>
|
2015-10-07 02:43:31 +00:00
|
|
|
<View style={styles.buttons}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<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)}
|
|
|
|
/>
|
2015-10-07 02:43:31 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-07 02:43:31 +00:00
|
|
|
|
|
|
|
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',
|
|
|
|
},
|
2016-01-29 09:16:40 +00:00
|
|
|
scrollStateText: {
|
|
|
|
color: '#99d1b7',
|
|
|
|
},
|
2015-10-07 02:43:31 +00:00
|
|
|
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;
|