Kis*_*ore 0 react-native react-native-flatlist
这是我的FlatList代码
<FlatList
ref={ref => this.flatListRef = ref}
data={this.state.data}
renderItem={({ item, index }) => this.renderItems(item, index)}
onScroll={(e) => this.handleScroll(e)}
/>
Run Code Online (Sandbox Code Playgroud)
这是handleScroll我得到的方法ScrollPosition
handleScroll(event) {
this.props.updateAppState('scrollPosition', event.nativeEvent.contentOffset.y);
}
Run Code Online (Sandbox Code Playgroud)
最后被componentDidMount ScrollToOffset称为
componentDidMount() {
this.flatListRef.scrollToOffset({ animated: false, offset: this.props.app.scrollPosition});
}
Run Code Online (Sandbox Code Playgroud)
FlatList从不滚动它的列表。我现在真的很无助。谁能告诉我我该怎么做才能达到scroll预期的效果offset?谢谢。
经过多次尝试,我找到了这个堆栈溢出答案的解决方案,即时间延迟使其起作用。
setTimeout(() => {
this.flatListRef.scrollToOffset({ offset: this.props.app.scrollPosition, animated: false })
}, 0)
Run Code Online (Sandbox Code Playgroud)
但我不太明白为什么需要这个时间延迟。官方文件本身没有提及任何有关时间延迟的内容。虽然问题现在已经解决,但我想知道延迟的原因。
有人能帮我知道这个时间延迟背后的原因吗?谢谢。
| 归档时间: |
|
| 查看次数: |
4543 次 |
| 最近记录: |