-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrolltest.js
154 lines (143 loc) · 3.67 KB
/
scrolltest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
TextInput,
TouchableOpacity,
} from 'react-native';
const FlatList = require('react-native/Libraries/CustomComponents/Lists/FlatList');
const listItems = [];
while (listItems.length < 10000) {
listItems.push({ number: listItems.length });
}
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
const listSource = ds.cloneWithRows(listItems);
const renderItem = ({ item }) => {
return (
<View style={styles.listItem}>
<Text>{item.number}</Text>
</View>
);
}
type ScrollTestState = {
inputText: string,
listType: number,
};
export default class ScrollTest extends Component {
state: ScrollTestState = {
inputText: '',
listType: 0,
};
render() {
let list = null;
if (this.state.listType === 0) {
list = (
<FlatList
ref={l => this.flatList = l}
data={listItems}
keyExtractor={l => l.number}
renderItem={renderItem}
shouldItemUpdate={() => false}
style={{ paddingHorizontal: 5 }}
/>
);
} else if (this.state.listType === 1) {
list = (
<ListView
dataSource={listSource}
renderRow={item => renderItem({ item })}
style={{ paddingHorizontal: 5 }}
/>
);
}
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<TextInput style={styles.input}
spellCheck={false}
autoCorrect={false}
value={this.state.inputText}
onChangeText={inputText => this.setState({ inputText })}
/>
<TouchableOpacity
style={styles.button}
onPress={() => this.setState({ inputText: '' })}
>
<Text style={styles.buttonText}>Clear</Text>
</TouchableOpacity>
</View>
<View style={styles.container}>
{list}
</View>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={() => this.setState({ listType: 0 })}
>
<Text style={styles.buttonText}>FlatList</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => this.setState({ listType: 1 })}
>
<Text style={styles.buttonText}>ListView</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => this.setState({ listType: 2 })}
>
<Text style={styles.buttonText}>Nothing</Text>
</TouchableOpacity>
{this.state.listType === 0 && (<TouchableOpacity
style={styles.button}
onPress={() => this.flatList.scrollToOffset({ animated: true, offset: 50000})}
>
<Text style={styles.buttonText}>Scroll</Text>
</TouchableOpacity>)}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
buttonContainer: {
flexDirection: 'row',
alignItems: 'center',
marginVertical: 20,
height: 55,
},
input: {
height: 55,
width: 200,
marginHorizontal: 5,
backgroundColor: '#ccffff',
},
button: {
flex: 1,
height: 40,
marginHorizontal: 5,
backgroundColor: '#cccccc',
justifyContent: 'center',
alignItems: 'center',
},
buttonText: {
fontSize: 18,
textAlign: 'center',
},
listItem:{
padding: 5,
borderRadius: 5,
backgroundColor: '#eeccee',
marginVertical: 3,
},
});