【问题标题】:react native FlatList scrollToItem error反应原生 FlatList scrollToItem 错误
【发布时间】:2018-08-20 17:30:24
【问题描述】:

我正在使用 React Native FlatList 组件。单击列表项时,它会向列表中添加更多项,并使用函数ScrollToItem 滚动到其中一项。 有时在滚动时我会在 android 上收到此错误:

scrollToIndex 应该与getItemLayout 或onScrollToIndexFailed 一起使用,否则无法知道离屏索引的位置或处理失败。

我尝试实现getItemLayout函数,但我的物品没有固定高度,而且我有多种不同高度的物品。

  • 也许有办法在内部处理这些情况 onScrollToIndexFailed?
  • 针对这种需求的最佳做法是什么?

【问题讨论】:

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


    【解决方案1】:

    是的,ScrollToItem 函数要求每个项目具有相同的高度。你可以使用函数scrollTo。

    示例:

    renderItem={({item})=>{<View onLayout={(e)=> console.log(e.nativeEvent.layout.y)}/>}} /*save each item location*/
    

    然后调用函数 scrollTo({x: 0, y: /* 你之前保存的 */, animated: true})

    希望对你有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-02
      • 2018-04-17
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多