我正在使用 React Native FlatList
成分。单击列表项时,它会向列表中添加更多项并使用函数 ScrollToItem
滚动到其中之一。 .
有时在滚动时,我会在 android 上收到此错误:
scrollToIndex should be used in conjunction with getItemLayout or onScrollToIndexFailed, otherwise there is no way to know the location of offscreen indices or handle failures.
我试图实现
getItemLayout
功能,但我的元素没有固定的高度,而且我有多种不同高度的元素。onScrollToIndexFailed
? 最佳答案
是的,ScrollToItem 函数要求每个项目具有相同的高度。您可以使用函数 scrollTo。
例子:
renderItem={({item})=>{<View onLayout={(e)=> console.log(e.nativeEvent.layout.y)}/>}} /*save each item location*/
然后调用函数 scrollTo({x: 0, y:/* 你之前保存的 */, animation: true})
希望这对你有用
关于react-native - react 原生 FlatList scrollToItem 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51935815/