【问题标题】:How to make a flat list scroll Infinitely in React Native With a Finite Amount of data in Array如何在 React Native 中使用有限数量的数组中的数据使平面列表无限滚动
【发布时间】:2020-02-10 11:55:41
【问题描述】:

我有水平设置的平面列表 来自数组的 11 项数据固定不变

我想要的是当用户在滚动时到达平面列表的末尾时,数据应该保持不变,但第一项应该 出现在最后,然后以此类推

这是我迄今为止尝试过的

  <FlatList
        {...this.props}
        ref={ref => {
          this.infListRef = ref;
        }}
        data={this.props.data}
        onScrollEndDrag={this.handleScroll}
        initialScrollIndex={0}
        scrollEventThrottle={16}
        renderItem={this.props.renderItem}
        onScroll={({nativeEvent}) => this.checkScroll(nativeEvent)}
        horizontal
        showsHorizontalScrollIndicator={false}
        keyExtractor={item => item.id}
      />

我们将不胜感激。

【问题讨论】:

    标签: react-native react-native-flatlist


    【解决方案1】:

    基本上,实现onScroll 用法是当您希望在实际滚动与滚动位置相关时引起注意(例如胺类)。当您希望在用户到达或即将到达FlatList 末尾时收到通知。

    您应该实现onEndReachedonEndReachedThreshold 以在用户达到阈值时处理更好的用户体验。

    您从源(服务器或任何来源)获取的新数据应连接到现有的this.props.data

    查看好博文 - https://scotch.io/tutorials/implementing-an-infinite-scroll-list-in-react-native

    这个 SO 回答 - React Native Infinite Scroll

    我的解决方案是分页,因为无限滚动是分页的私人案例,它是完全相同的方法。

    【讨论】:

    • 兄弟感谢您的回答,但问题要求数据是有限的!没有分页!只有 11 项重复
    • Infinite 是分页的私有案例,完全一样的做法。
    【解决方案2】:

    如果你想要imagevideo 列表 另一种比较简单的方法是使用react-native-snap-carousel

    <Carousel
          ref={ (c) => { this._carousel = c; } }
          data={this.state.data}
          renderItem={this._renderItem.bind(this)}
          onSnapToItem={this.handleSnapToItem.bind(this)}
          sliderWidth={360}
          itemWidth={256}
          layout={'default'}
          firstItem={0}
          itemHeight={20}
          sliderHeight={20}
          loop
          vertical
          loopClonesPerSide={100}
        />
    

    示例: https://snack.expo.io/@kurtesy/react-native-snap-carousel-example

    【讨论】:

      【解决方案3】:

      您可以使用FlatlistonEndReached 方法实现此目的。

      这就是答案背后的想法。

          state = {
            data: []   //your initial data
          }
      
      
          <Flatlist
           {…this.props}
           extraData={this.state}
           onEndReached = {() => {
           this.setState((prevState) =>{
              data: […prevState,this.state.data]
           )}}
          />
      

      【讨论】:

      • 这不是问题的正确答案。它不是循环有限数据项,而是一遍又一遍地添加数据(使其无限)导致内存泄漏。尝试让一个显示您拥有的实际物品数量,或者在滚动几卷后添加一些物品,看看会发生什么
      • 是的,这导致了我的数据问题,但您能提出一些更好的解决方案吗?
      猜你喜欢
      • 2022-12-20
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多