react-native - react 原生 FlatList scrollToItem 错误

标签 react-native react-native-android react-native-ios react-native-flatlist

我正在使用 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/

    相关文章:

    react-native - React Native Toolbar Android 溢出图标是黑色的——我可以更改它的颜色还是需要完全替换它?

    react-native - 为什么 shadowColor : '...' - is not applied for drawer styles on Android of “react-native-drawer” component?

    ios - react native iOS : Could not build module 'yoga' : 'algorithm' file not found

    javascript - 如何从导入调用异步函数?

    javascript - 将元素放置在 react native 的右上角

    react-native - 导航栏下的 React Native 导航状态栏

    ios - react native iOS 构建问题在提供的路径中找不到应用程序包

    javascript - 渲染两个导航器

    react-native - 使用插值时如何初始设置不透明度

    amazon-web-services - 如何在 React Native (Android) 中订阅 AWS SNS 主题?