借鉴该地址: https://stackoverflow.com/questions/51098599/flatlist-inside-scrollview-doesnt-scroll
代码如下: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
55import { Component, default as React } from 'react';
import { View, FlatList, ScrollView, Text } from 'react-native';
export default class LabScreen extends Component<{}> {
constructor(props) {
super(props);
this.state = {enableScrollViewScroll: true};
}
render() {
return (
<View
onStartShouldSetResponderCapture={() => {
this.setState({ enableScrollViewScroll: true });
}}>
<ScrollView
scrollEnabled={this.state.enableScrollViewScroll}
ref={myScroll => (this._myScroll = myScroll)}>
{this.renderFlatList('red')}
{this.renderFlatList('green')}
{this.renderFlatList('purple')}
{this.renderFlatList('pink')}
</ScrollView>
</View>
);
}
getRandomData = () => {
return new Array(100).fill('').map((item, index) => {
return { title: 'Title ' + (index + 1) };
});
};
renderFlatList(color: string) {
return (
<View
onStartShouldSetResponderCapture={() => {
this.setState({ enableScrollViewScroll: false });
if (this._myScroll.contentOffset === 0
&& this.state.enableScrollViewScroll === false) {
this.setState({ enableScrollViewScroll: true });
}
}}>
<FlatList
data={this.getRandomData()}
backgroundColor={color}
maxHeight={200}
marginBottom={50}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => <Text>{item.title}</Text>}
/>
</View>
);
}
}