【问题标题】:Setting itemHeight(for vertical carousel) as per content size while using react-native-snap-carousel?在使用 react-native-snap-carousel 时根据内容大小设置 itemHeight(用于垂直轮播)?
【发布时间】:2023-12-31 10:52:01
【问题描述】:
我正在尝试使用 react-native-snap-carousel 实现垂直旋转木马。我想要实现的是一个垂直旋转木马,其旋转木马高度根据内容而异。根据 react-native-snap-carousel itemHeight 的文档当涉及到垂直轮播时,sliderHeight 是必需的。那么有没有我可以根据内容大小达到 itemHeight 的呢?
【问题讨论】:
标签:
reactjs
react-native
carousel
native
react-native-snap-carousel
【解决方案1】:
据我了解,您希望获取内容的视图高度并将其提供给轮播项目。您可以通过使用它们的 refs 来获取项目的位置和大小属性。
首先,从 react 中导入 useRef。
import { useRef } from 'react';
之后,创建一个 ref 值。
const contentRef = useRef(null);
<View ref={contentRef}> .... </View>
之后,你可以通过
获取它的属性
contentRef.nativeEvent.measureInWindow((x, y, width, height) => {
// You can do whatever you want with this data
})