首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在FlatList react本机(均值-10-10条记录)中手动添加带有旋转器的加载更多记录!不是因为使用服务器端

如何在FlatList react本机(均值-10-10条记录)中手动添加带有旋转器的加载更多记录!不是因为使用服务器端
EN

Stack Overflow用户
提问于 2018-05-03 08:57:52
回答 1查看 21.2K关注 0票数 15

嗨,我正在开发基于FlatList的示例应用程序,这里是我的代码。实际上,我把所有的记录都展示给了我的账户,好像我有50张记录。但现在我展示了全部50张唱片。我需要在添加10张唱片后显示10张。但我不知道加入FlatList。

这是我的代码:

代码语言:javascript
复制
<FlatList
                    data={this.state.profiles}
                    renderItem={({ item, index }) => this.renderCard(item, index)}
                    keyExtractor={item => item.id}
                    ItemSeparatorComponent={() => <Divider style={{ marginTop: 5, marginLeft: width * 0.2 + 20 }} parentStyle={{ backgroundColor: globalStyles.BG_COLOR, alignItems: 'baseline' }} />}
                />


renderCard (profile, index) {
    console.log('rendercard', profile);
    //
    return (
        <View key={profile.id}>
            <ProfileCard
                profile={profile}
                style={styles.card}
                onPress={() => this.props.screenProps.rootNavigation.navigate('Profile', { profile: this.state.profile, id: profile.id })}
                // onPress={() => alert('PROFILE')}
                onAddClick={() => this.setState({ connectionPageVisible: true, cardProfile: profile })}
                connectedIds={(this.props.screenProps && this.props.screenProps.connectedIds) || this.props.connectedIds}
            />
        </View>
    );
}

请向我显示更多的记录与活动指示。提前谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-05-03 15:07:24

如果我已经正确地理解了您的问题,那么您正在寻找infinite scrolling in Flatlist。您可以借助onEndReachedonEndThreshold属性来实现这一点。

考虑下面的原型

假设您要将记录存储到this.state.profiles中。

从服务器中提取新记录

在构造函数中设置初始页码

代码语言:javascript
复制
constructor(props){
   super(props);
   this.state = { page: 0}
}

获取新记录

代码语言:javascript
复制
fetchRecords = (page) => {
    // following API will changed based on your requirement
    fetch(`${API}/${page}/...`)
    .then(res => res.json())
    .then(response => {
       this.setState({
           profiles: [...this.state.profiles, ...response.data] // assuming response.data is an array and holds new records
       });
    });
}

处理卷轴

代码语言:javascript
复制
onScrollHandler = () => {
     this.setState({
        page: this.state.page + 1
     }, () => {
        this.fetchRecords(this.state.page);
     });
}

渲染功能

代码语言:javascript
复制
render() {
    return(
        ...
        <FlatList
           data={this.state.profiles}
           renderItem={({ item, index }) => this.renderCard(item, index)}
           keyExtractor={item => item.id}
           ItemSeparatorComponent={() => <Divider style={{ marginTop: 5, marginLeft: width * 0.2 + 20 }} parentStyle={{ backgroundColor: globalStyles.BG_COLOR, alignItems: 'baseline' }} />}
           onEndReached={this.onScrollHandler}
           onEndThreshold={0}
        />
        ...
    );
}

本地更新

如果您已经提取了所有数据,但希望一次只显示10,则只需更改fetchRecords即可。

代码语言:javascript
复制
fetchRecords = (page) => {
  // assuming this.state.records hold all the records
  const newRecords = []
  for(var i = page * 10, il = i + 10; i < il && i < this.state.records.length; i++){
      newRecords.push(this.state.records[i]);
  }
  this.setState({
    profiles: [...this.state.profiles, ...newRecords]
  });
}

以上方法将显示Activity Indicator同时提取记录。

希望这能帮上忙!

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50150911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档