Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在React Native中加载屏幕时如何滚动到顶部?_Android_Reactjs_React Native_React Native Flatlist - Fatal编程技术网

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 ]}
/>