2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-03-23 22:07:33 +00:00
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule ScrollViewExample
|
2018-01-13 03:07:17 +00:00
|
|
|
* @format
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2015-01-30 01:10:49 +00:00
|
|
|
var {
|
2017-06-06 20:00:30 +00:00
|
|
|
Platform,
|
2015-01-30 01:10:49 +00:00
|
|
|
ScrollView,
|
|
|
|
StyleSheet,
|
2016-02-03 11:59:20 +00:00
|
|
|
Text,
|
|
|
|
TouchableOpacity,
|
2015-01-30 01:10:49 +00:00
|
|
|
View,
|
2018-01-13 03:07:17 +00:00
|
|
|
Image,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-02-07 04:58:23 +00:00
|
|
|
exports.displayName = 'ScrollViewExample';
|
2015-01-30 01:10:49 +00:00
|
|
|
exports.title = '<ScrollView>';
|
2018-01-13 03:07:17 +00:00
|
|
|
exports.description =
|
|
|
|
'Component that enables scrolling through child components';
|
2015-01-30 01:10:49 +00:00
|
|
|
exports.examples = [
|
2018-01-13 03:07:17 +00:00
|
|
|
{
|
|
|
|
title: '<ScrollView>',
|
|
|
|
description:
|
|
|
|
'To make content scrollable, wrap it within a <ScrollView> component',
|
|
|
|
render: function() {
|
2017-02-07 04:58:23 +00:00
|
|
|
var _scrollView: ScrollView;
|
|
|
|
return (
|
2018-01-13 03:07:17 +00:00
|
|
|
<View>
|
2017-02-07 04:58:23 +00:00
|
|
|
<ScrollView
|
2018-01-13 03:07:17 +00:00
|
|
|
ref={scrollView => {
|
|
|
|
_scrollView = scrollView;
|
|
|
|
}}
|
2017-02-07 04:58:23 +00:00
|
|
|
automaticallyAdjustContentInsets={false}
|
2018-01-13 03:07:17 +00:00
|
|
|
onScroll={() => {
|
|
|
|
console.log('onScroll!');
|
|
|
|
}}
|
|
|
|
scrollEventThrottle={200}
|
|
|
|
style={styles.scrollView}>
|
2017-02-07 04:58:23 +00:00
|
|
|
{THUMB_URLS.map(createThumbRow)}
|
|
|
|
</ScrollView>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.button}
|
2018-01-13 03:07:17 +00:00
|
|
|
onPress={() => {
|
|
|
|
_scrollView.scrollTo({y: 0});
|
|
|
|
}}>
|
|
|
|
<Text>Scroll to top</Text>
|
2017-02-07 04:58:23 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.button}
|
2018-01-13 03:07:17 +00:00
|
|
|
onPress={() => {
|
|
|
|
_scrollView.scrollToEnd({animated: true});
|
|
|
|
}}>
|
|
|
|
<Text>Scroll to bottom</Text>
|
2017-02-07 04:58:23 +00:00
|
|
|
</TouchableOpacity>
|
2017-09-27 23:03:23 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.button}
|
2018-01-13 03:07:17 +00:00
|
|
|
onPress={() => {
|
|
|
|
_scrollView.flashScrollIndicators();
|
|
|
|
}}>
|
2017-09-27 23:03:23 +00:00
|
|
|
<Text>Flash scroll indicators</Text>
|
|
|
|
</TouchableOpacity>
|
2017-02-07 04:58:23 +00:00
|
|
|
</View>
|
|
|
|
);
|
2018-01-13 03:07:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '<ScrollView> (horizontal = true)',
|
|
|
|
description:
|
|
|
|
"You can display <ScrollView>'s child components horizontally rather than vertically",
|
|
|
|
render: function() {
|
|
|
|
function renderScrollView(
|
|
|
|
title: string,
|
|
|
|
addtionalStyles: typeof StyleSheet,
|
|
|
|
) {
|
|
|
|
var _scrollView: ScrollView;
|
|
|
|
return (
|
|
|
|
<View style={addtionalStyles}>
|
|
|
|
<Text style={styles.text}>{title}</Text>
|
|
|
|
<ScrollView
|
|
|
|
ref={scrollView => {
|
|
|
|
_scrollView = scrollView;
|
|
|
|
}}
|
|
|
|
automaticallyAdjustContentInsets={false}
|
|
|
|
horizontal={true}
|
|
|
|
style={[styles.scrollView, styles.horizontalScrollView]}>
|
|
|
|
{THUMB_URLS.map(createThumbRow)}
|
|
|
|
</ScrollView>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.button}
|
|
|
|
onPress={() => {
|
|
|
|
_scrollView.scrollTo({x: 0});
|
|
|
|
}}>
|
|
|
|
<Text>Scroll to start</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.button}
|
|
|
|
onPress={() => {
|
|
|
|
_scrollView.scrollToEnd({animated: true});
|
|
|
|
}}>
|
|
|
|
<Text>Scroll to end</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.button}
|
|
|
|
onPress={() => {
|
|
|
|
_scrollView.flashScrollIndicators();
|
|
|
|
}}>
|
|
|
|
<Text>Flash scroll indicators</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2017-02-07 04:58:23 +00:00
|
|
|
|
2018-01-13 03:07:17 +00:00
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
{renderScrollView('LTR layout', {direction: 'ltr'})}
|
|
|
|
{renderScrollView('RTL layout', {direction: 'rtl'})}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class Thumb extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2015-01-30 01:10:49 +00:00
|
|
|
return false;
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2015-01-30 01:10:49 +00:00
|
|
|
return (
|
2017-02-07 04:58:23 +00:00
|
|
|
<View style={styles.thumb}>
|
|
|
|
<Image style={styles.img} source={this.props.source} />
|
2015-01-30 01:10:49 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-02-07 04:58:23 +00:00
|
|
|
var THUMB_URLS = [
|
|
|
|
require('./Thumbnails/like.png'),
|
|
|
|
require('./Thumbnails/dislike.png'),
|
|
|
|
require('./Thumbnails/call.png'),
|
|
|
|
require('./Thumbnails/fist.png'),
|
|
|
|
require('./Thumbnails/bandaged.png'),
|
|
|
|
require('./Thumbnails/flowers.png'),
|
|
|
|
require('./Thumbnails/heart.png'),
|
|
|
|
require('./Thumbnails/liking.png'),
|
|
|
|
require('./Thumbnails/party.png'),
|
|
|
|
require('./Thumbnails/poke.png'),
|
|
|
|
require('./Thumbnails/superlike.png'),
|
|
|
|
require('./Thumbnails/victory.png'),
|
|
|
|
];
|
|
|
|
|
|
|
|
THUMB_URLS = THUMB_URLS.concat(THUMB_URLS); // double length of THUMB_URLS
|
|
|
|
|
|
|
|
var createThumbRow = (uri, i) => <Thumb key={i} source={uri} />;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
scrollView: {
|
2017-02-07 04:58:23 +00:00
|
|
|
backgroundColor: '#eeeeee',
|
2015-01-30 01:10:49 +00:00
|
|
|
height: 300,
|
|
|
|
},
|
|
|
|
horizontalScrollView: {
|
2017-02-07 04:58:23 +00:00
|
|
|
height: 106,
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
text: {
|
2017-02-07 04:58:23 +00:00
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
margin: 5,
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
button: {
|
2017-02-07 04:58:23 +00:00
|
|
|
margin: 5,
|
2015-01-30 01:10:49 +00:00
|
|
|
padding: 5,
|
|
|
|
alignItems: 'center',
|
2017-02-07 04:58:23 +00:00
|
|
|
backgroundColor: '#cccccc',
|
2015-01-30 01:10:49 +00:00
|
|
|
borderRadius: 3,
|
|
|
|
},
|
2017-02-07 04:58:23 +00:00
|
|
|
thumb: {
|
|
|
|
margin: 5,
|
|
|
|
padding: 5,
|
|
|
|
backgroundColor: '#cccccc',
|
|
|
|
borderRadius: 3,
|
|
|
|
minWidth: 96,
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
img: {
|
|
|
|
width: 64,
|
|
|
|
height: 64,
|
2018-01-13 03:07:17 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
});
|