首页 > 解决方案 > React Native FlatList inside ScrollView onEndReached 从未调用过

问题描述

我的容器是一个滚动视图,里面是一个平面列表,它从服务器加载数据。

平面清单:

<VirtualizedList
  ListEmptyComponent={<NoData />}
  data={data}
  getItem={(items, index) => items.get(index)}
  getItemCount={(items) => items.size}
  keyExtractor={(item, index) => String(index)}
  renderItem={this._renderItem}
  refreshControl={
     <RefreshControl
       refreshing={loading}
       onRefresh={this._onRefresh.bind(this)}
     />
  }
  onEndReached={this.handleLoadMore}
  onEndReachedThreshold={0.1}
  onMomentumScrollBegin={() => {
    Log('onMomentumScrollBegin fired!')
    this.onEndReachedCalledDuringMomentum = false;
  }}
/>

handleLoadMore是:

handleLoadMore = () => {
  Log('handleLoadMore fired!');
  if (!this.onEndReachedCalledDuringMomentum) {
    // fetch data..
    this.onEndReachedCalledDuringMomentum = true;
  }
}

问题是handleLoadMore从未被调用和onMomentumScrollBegin也从未被调用。

如何解决这个问题?

标签: react-nativereact-native-flatlist

解决方案


实际上你不需要使用ContentorScrollView作为 FlatList 两者ListFooterComponent兼而有之ListHeaderComponent

如果您确实需要在内部使用 FlatList ScrollView,则将样式和内容 contentContainerStyle 添加到您的ScrollView或者如果您使用 native-base,则在Content

<ScrollView
  ...
  style={{flex: 1}}
  contentContainerStyle={{flex: 1}}
  ...
/>

然后,如果您想在 FlatList 循环之外使用标头组件。然后ListHeaderComponent={() => <SomeHeader />在 Flatlist 中使用。


推荐阅读