【问题标题】:In React-Native FlatList onScroll event is not getting called在 React-Native FlatList onScroll 事件没有被调用
【发布时间】:2020-01-04 04:13:21
【问题描述】:

对于以下代码,FlatList onScroll 事件根本没有触发:

return (
  <View style={styles.container}>
    <FlatList 
      keyboardShouldPersistTaps={'always'} 
      data={data} 
      onScroll={() => { 
        console.log('test');
        Keyboard.dismiss; //FlatList is not firing onScroll event 
      }} 
      renderItem={({ item, index }) => ( 
        <ListItem item={item} index={index} /> 
      )} 
      keyExtractor={item => item.key} 
    />
  </View>
);

【问题讨论】:

  • 你能显示完整的代码吗?
  • 更新了#Anuj Sharma
  • 也许您的 Keyboard.dismiss 功能不起作用....
  • 这是一个已知问题。我也厌倦了尝试许多解决方案。它在 IOS 中触发,但在 android 中不触发。我最终按照我的要求使用了 onEndReached。
  • onScroll 事件本身没有被调用,因为即使 console.log 也没有被调用

标签: react-native react-native-flatlist onscroll


【解决方案1】:

使用

OnEndReached

平面列表的功能。如果您在 Flatlist 中使用 onScroll,那么有时它不适用于 android,但适用于 iOS。让我们看看下面的代码:

        <FlatList
            // style = {{marginBottom: 100}}
            data={feedbacks}
            showsVerticalScrollIndicator={false}
            keyExtractor={f => f._id.toString()}
            renderItem={renderFeedback}
            onEndReached={() => console.log("End reached")}
            onEndReachedThreshold={0}
            // onScroll={({nativeEvent}) => { // not working on android
            //  if (!loading && isCloseToBottom(nativeEvent)) {
            //    setPageNo(page=> page+1)
            //  }
            //   }}
            // scrollEventThrottle={500}
            ListFooterComponent={() => loading ?
                <ActivityIndicator
                    style={{ padding: 15 }}
                    size="large"
                    color={colors.primary}
                />
                : null
            }
        />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 2019-03-29
    • 2023-03-29
    • 2023-02-04
    • 2021-11-03
    相关资源
    最近更新 更多