【问题标题】:FlatList infinity scrolling on my code is not working我的代码上的 FlatList 无限滚动不起作用
【发布时间】:2019-05-15 14:18:21
【问题描述】:

我正在使用 RN 开发应用程序,其中一个组件是数据列表,我执行平面列表的集成以形成列表...

<FlatList data={this.state.tickets}
         keyExtractor={(item,index) => item._id + Math.random()}
         refreshing={this.state.refreshing}
         onRefresh={this.handleRefresh}
         onEndReached={this.handleLoadMore}
         onEndReachedThreshold={0.5}
         ListFooterComponent={this.footerList}
         renderItem={({item}) => 
             <View style={styles.childOneFlat}>
                 <View style={styles.childTwoFlat}>
                     <TouchableOpacity style={styles.childThreeFlat} 
                         onPress={() => {this.goToTask(item)}}>
                             <View style={styles.childFourFlat}>
                                 <Text style={styles.childFiveFlat}> 
                                 {'Ticket N° '+item.taskId}</Text>
                                 <Text style={styles.childSixFlat}>></Text>
                             </View>
                    </TouchableOpacity>
                </View>            
            </View>}>
</FlatList>

抱歉缩进不好...

我的方法:

handleRefresh = () => {
    this.setState({
        page:1,
        refreshing:true
    }, () => {
        this.getTickets();
    });
};

handleLoadMore = () => {
    this.setState((prevStates,nextProps) => ({page: prevStates + 1, 
    loadingMore:true}), () => {this.getTickets();});
};

renderFooter = () => {
    if (!this.state.loadingMore) return null;

    return (
      <View
        style={{
          position: 'relative',
          width: width,
          height: height,
          paddingVertical: 20,
          borderTopWidth: 1,
          marginTop: 10,
          marginBottom: 10,
          borderColor: colors.veryLightPink
        }}
      >
        <ActivityIndicator animating size="large" color='blue'/>
      </View>
    );
  };

handleRefresh = () => {
    this.setState(
        {
            page: 1,
            refreshing: true
        },
        () => {
            this._fetchAllBeers();
        }
    );
};

有人可能会看到我找不到的错误。

我希望能够在列表中加入无限滚动,感谢从现在开始回答的人...

【问题讨论】:

  • handleLoadMore 在组件加载时触发,之后无法再次触发

标签: javascript react-native redux react-native-android react-native-flatlist


【解决方案1】:

您是否检查过您的 data 道具是否正在更新? this.state.tickets 应该始终附加从 this.getTickets(); API 调用接收到的下一个项目,而不是重写整个数组。

【讨论】:

  • 对了,我的做法?... (getState().ticketList.tickets !== null) { ... }
  • 我不明白。你能详细说明你在哪里写这段代码吗?
  • 我仍然有由此产生的问题...我正在更改组件的状态,在以前的状态中,我有一个包含 20 个元素的数组,在下一个是 40 个元素中...但是我仍然没有在平面列表中显示它们的渲染
  • 尝试更改onEndReachedThreshold 值。
  • 是否通过更改onEndReachedThreshold 值解决了问题?如果是这样,我会编辑答案,以便它可以帮助其他人。
猜你喜欢
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 2013-07-05
  • 2018-10-22
  • 1970-01-01
相关资源
最近更新 更多