我目前有一个地图和一个轮播(类似于FlatList),我希望能够使用FlatList的scrollToIndex方法来捕捉到数组中的某个项目。问题是,当我采用基于ScrollView的react-native-snap-carousel库时,我失去了使用scrollToIndex方法的能力。滚动到某个索引的功能在FlatList上运行得非常好。
我使用了一个功能组件,所以我使用了useRef钩子:
const flatListRef = useRef()
const handleMarker = index => {
flatListRef.current.scrollToIndex({ index, animated: true })
}我为我的地图使用react-native-maps,上面的handleMarker是这样的,当用户点击地图上的一个标记时,旋转木马捕捉到相关的项目:
<Marker
key={marker.id}
coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
onPress={() => handleMarker(index)}
ref={markerRef[index]}
>
</Marker>以下是取代FlatList的旋转木马
<Carousel
data={items}
renderItem={({ item }) => <Item item={item}/>}
horizontal={true}
ref={flatListRef}
sliderWidth={width}
sliderHeight={100}
itemWidth={140}
itemHeight={100}
/>如何在没有scrollToIndex的情况下实现这一点?
发布于 2019-12-24 08:27:24
请尝试使用所提供的方法
const handleMarker = index => {
flatListRef.current.snapToItem(index);
}https://stackoverflow.com/questions/59462489
复制相似问题