【问题标题】:Multiple Carousel Pagination sync issue inside Infinite FlatListInfinite FlatList 中的多个轮播分页同步问题
【发布时间】:2021-08-25 16:32:00
【问题描述】:

Reactive Native Expo Mobile App:我正在使用React Native Snap Carousel using this tutorial 从我的数据库中弹出图像。

我有一个无限的平面列表,其中包含单个条目作为 React Native Snap Carousels,因此有多个带有多个分页的轮播。

在有多个轮播时,分页索引现在普遍移动,因为所有分页都使用相同的索引和轮播引用。

请告诉我如何映射轮播引用和分页索引,以便我的 FlatList 中的多个轮播正常工作。

我正在使用的代码,供参考:

const isCarousel = React.useRef(null);
const [index, setIndex] = React.useState(0);

const renderItem = ({item}) => {
return(
                // Some Card Stuff...
                <Carousel
                layout="tinder"
                layoutCardOffset={9}
                ref={isCarousel}
                data={photoArray}
                renderItem={CarouselCardItem}
                sliderWidth={SLIDER_WIDTH}
                itemWidth={ITEM_WIDTH}
                inactiveSlideShift={0}
                onSnapToItem={(index) => setIndex(index)}
                useScrollView={true}
              />
              <Pagination
                      dotsLength={photoArray.length}
                      activeDotIndex={index}
                      carouselRef={isCarousel}
                      dotStyle={{
                        width: 10,
                        height: 10,
                        borderRadius: 5,
                        marginHorizontal: 0,
                        backgroundColor: 'rgba(0, 0, 0, 0.92)'
                      }}
                      inactiveDotStyle={{
                        backgroundColor: 'rgba(4, 5, 3, 0.92)'
                      }}
                      inactiveDotOpacity={0.4}
                      inactiveDotScale={0.6}
                      tappableDots={true}
                    />
// Some more Stuff....
)

还有主要的 FlatList:

return (
        <FlatList 
        data={data_data} 
        renderItem={renderItem}
        keyExtractor = {item => `${item.identifier}`}
        ListFooterComponent = {Loader}
        onEndReached = {loadStuff}
        onRefresh = {() => refreshData()}
        refreshing = {isRefreshing}
/>
)

【问题讨论】:

    标签: react-native jsx react-native-snap-carousel


    【解决方案1】:

    使用您的代码制作自定义轮播组件

    const MyCarousel =({item}) => {
       const isCarousel = React.useRef(null);
       const [index, setIndex] = React.useState(0);
       
       return (
         <React.Fragment>
         <Carousel
            layout="tinder"
            layoutCardOffset={9}
            ref={isCarousel}
            data={photoArray}
            renderItem={CarouselCardItem}
            sliderWidth={SLIDER_WIDTH}
            itemWidth={ITEM_WIDTH}
            inactiveSlideShift={0}
            onSnapToItem={(index) => setIndex(index)}
            useScrollView={true}/>
            <Pagination
              dotsLength={photoArray.length}
              activeDotIndex={index}
              carouselRef={isCarousel}
              dotStyle={{
               width: 10,
               height: 10,
               borderRadius: 5,
               marginHorizontal: 0,
               backgroundColor: 'rgba(0, 0, 0, 0.92)'
              }}
              inactiveDotStyle={{ backgroundColor: 'rgba(4, 5, 3, 0.92)' }}
              inactiveDotOpacity={0.4}
              inactiveDotScale={0.6}
              tappableDots={true}/>
        </React.Fragment>
       )
    }
    

    然后像下面这样使用它

    const renderItem = ({item}) => {
       return(
        <View>
         <MyCarousel item={item}/> 
         // Some more Stuff....
        </View>
       );
    }
    

    【讨论】:

    • 我正在尝试这个,但是 ...> 吗?
    • 通过在 React.Fragment 下包装轮播代码解决了上述错误。请接受编辑,我可以将其标记为答案。
    • 这种方法只有一个问题。每当平面列表在结束时重新加载时,为了获得更多内容,所有轮播和分页都将重置为第一张照片/索引。
    • 因为当你更新状态(列表)时,屏幕会重新绘制。
    • 通过将其他卡部件分离到它们自己的组件中来解决上述问题。现在我还需要在卡中嵌入 YouTube 视频,我发现这很难做到......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多