react-native - 如何在 React Native 的 renderItem 函数中获取 FlatList 元素的高度

标签 react-native

我是 React Native 的新手。我想在向左滑动时为 FlatList 项的删除设置动画。我正在获取滑动值并能够删除该项目,但它并不流畅。

因此,我正在使用 React Native 的 Animated API,并希望在滑动后的 1000 毫秒内使 FlatList 项目的高度变为 0。

我正在使用 inputRange 和 outputRange。但是我无法获得当前元素的高度,因为所有元素的高度都是动态的。

最佳答案

您可以使用 onLayout 属性获取 View 的高度。

<View onLayout={(event) => {
  const {x, y, width, height} = event.nativeEvent.layout;
  // do something here like set your initial animated value for the height
}} />

https://facebook.github.io/react-native/docs/view#onlayout

动画高度不是最好的方法,因为高度动画的性能不佳。您可能希望为变换设置动画,并为 View 的垂直比例设置动画。

如果您可以使用 Native 驱动程序进行动画处理,那么它们的性能会非常好 https://facebook.github.io/react-native/docs/animations#using-the-native-driver

Not everything you can do with Animated is currently supported by the native driver. The main limitation is that you can only animate non-layout properties: things like transform and opacity will work, but flexbox and position properties will not.

这个 SO 答案很好地解释了如何执行变换动画 https://stackoverflow.com/a/45634834/5508175

我个人的做法是淡出 View 的内容,然后将 View 的垂直比例设置为零。

关于react-native - 如何在 React Native 的 renderItem 函数中获取 FlatList 元素的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54334666/

相关文章:

javascript - 尝试让 Firebase 电子邮件/密码身份验证与 React Native 一起使用

javascript - 如何使用react-native-fetch-blob将文件从应用程序文档文件夹移动到Iphone的文档文件夹

react-native - React Native 应用程序具有相同的代码,但在 iOS 和 Android 中结果不同

javascript - 在ScrollView问题中 react Native FlatList

react-native - 在 react-navigation 5.x 选项卡中预加载嵌套堆栈

javascript - React Native - 在同一个对象中引用键?

react-native - 将属性声明为样式时,Eslint react/no-typos 规则会引发错误

reactjs - Redux 形式,提供给 'value' 的类型 'number' 的无效 prop 'TextInput' ,预期为 'string'

javascript - React Native 模块向 JavaScript 文档发送事件是否正确?

react-native - XDLError : ValidationError: "scheme" is not allowed on Expo 错误