gpt4 book ai didi

react-native - react 原生 FlatList scrollToItem 错误

转载 作者:行者123 更新时间:2023-12-04 04:21:12 28 4
gpt4 key购买 nike

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

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com