简体   繁体   English

ScrollView 属性“onScroll”只触发一次

[英]ScrollView property 'onScroll' only fires once

I'm trying to get this extremely simple React Native (Expo) Code to work:我试图让这个非常简单的 React Native (Expo) 代码工作:

Inside my App.js:在我的 App.js 中:

return (
  <ScrollView
    scrollEventThrottle={16}
    onScroll={console.log("Scrolled")}>
    <Text style={{fontSize: 30}}>
      ...
    </Text>
  </ScrollView>
)

It seems as if onScroll would only be fired once when I'm first starting the App, but is not responding to actual scrolling.似乎onScroll只会在我第一次启动应用程序时被触发一次,但没有响应实际滚动。 Any ideas on what could be blocking it?关于什么可能阻止它的任何想法?

(using Expo version 3.17.18) (使用 Expo 版本 3.17.18)

I'm guessing the way that the onScroll is written could be causing it to never trigger it more than once.我猜 onScroll 的编写方式可能导致它永远不会多次触发它。 Please try the following code (I would also recommend having an scrollEventThrottle of 400 or so, 16 will be triggered far too many times, but I wouldn't say that's the root of the current issue):请尝试以下代码(我还建议将scrollEventThrottle为 400 左右,16 会被触发太多次,但我不会说这是当前问题的根源):

 return (
  <ScrollView
    scrollEventThrottle={16}
    onScroll={({nativeEvent}) => {console.log("Scrolled")}}>
    <Text style={{fontSize: 30}}>
      ...
    </Text>
  </ScrollView>
)

One of the main reason of this is not using scrollEventThrottle as I can see you have already used it.主要原因之一是没有使用scrollEventThrottle ,因为我可以看到您已经使用过它。 because you need to specify how often you want to get the scrolling information by passing an additional scrollEventThrottle props with a minimum value of 16 (to get the event every 16ms, which amounts to once for every frame in 60fps)因为您需要通过传递一个最小值为 16 的附加 scrollEventThrottle 道具来指定您希望获取滚动信息的频率(每 16 毫秒获取一次事件,相当于 60fps 中的每一帧一次)

If you want to implement infinite scrolling you might be better of with a FlatList.如果要实现无限滚动,最好使用 FlatList。 Because the React Native Documentation says that ScrollViews are better for a small amount of items因为React Native文档说 ScrollViews 更适合少量项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM