Android 在React Native中加载屏幕时如何滚动到顶部?
我在主屏幕上使用Android 在React Native中加载屏幕时如何滚动到顶部?,android,reactjs,react-native,react-native-flatlist,Android,Reactjs,React Native,React Native Flatlist,我在主屏幕上使用Flatlist,它显示了多篇文章。所以现在我想要的是,无论何时我从应用程序注销或关闭应用程序,然后打开应用程序,我都应该能够看到Flatlist中的第一项 我尝试在渲染函数中使用scrollToIndex,但它给了我一个错误-undefined不是对象(计算'\u this2.refs.flatListRef.scrollToIndex') {this.flatListRef=ref;} renderItem={({item,index})=>this.\u renderIte
Flatlist
,它显示了多篇文章。所以现在我想要的是,无论何时我从应用程序注销或关闭应用程序,然后打开应用程序,我都应该能够看到Flatlist
中的第一项
我尝试在渲染函数中使用scrollToIndex
,但它给了我一个错误-undefined不是对象(计算'\u this2.refs.flatListRef.scrollToIndex')
{this.flatListRef=ref;}
renderItem={({item,index})=>this.\u renderItem(item,index)}
extraData={[this.state.data,this.state.checked]}
/>
这就是我在componentDidMount
和内部渲染函数this.refs.flatListRef.scrollToIndex({animated:true,index:0})中尝试使用的内容代码>但不起作用。在用户离开应用程序并稍后恢复应用程序后,ComponentDidMount回调将不会运行。您必须改为使用:
AppState可以告诉您应用程序是在前台还是后台,并在状态更改时通知您
根据您的需要调整给定的示例,并使用this.flatListRef.scrollToIndex({animated:true,index:0})}
this.flatListRef.scrollToIndex({animated:true,index:0})
应该可以修复它,因为您正在使用ref作为回调。@PritishVaidya我尝试在Flatlist的呈现函数中使用它,但它仍然不起作用。我尝试使用Official React本机文档中使用的AppState,但AppState
的addEventListener
处理程序(函数)似乎不起作用。你能告诉我使用AppState的正确方法吗?
<FlatList
data={this.state.data}
ref={(ref) => { this.flatListRef = ref; }}
renderItem={ ({item,index}) => this._renderItem(item,index) }
extraData={[ this.state.data, this.state.checked ]}
/>