首页 > 解决方案 > 无限循环无法使用 loop={true} , enableSnap ={true} 列表未在 android 中使用 react-native-snap-carousel 滚动无限模式

问题描述

实际行为

我在卡片中显示列表并且卡片没有在无限模式下滚动它在android中间被击中

预期行为

在android中滚动时,卡片应该在无限模式下滚动

<Carousel
         ref={c => {
           this._carousel = c;
         }}
         carouselRef={this.props._slider1Ref}
         data={this.props.data}
         renderItem={this._renderItem}
         sliderWidth={this.props.sliderWidth}
         itemWidth={this.props.itemWidth}
         loop={true}
         loopClonesPerSide={this.props.loopClonesPerSide}
         firstItem={this.props.firstItem}
         onSnapToItem={index => this.onSnap(index)}
         layout={this.props.layout}
         layoutCardOffset={this.props.layoutCardOffset}
         showSpinner={this.props.showSpinner}
         activeSlideAlignment="start"
         useScrollView={false}
         activeSlideOffset={this.props.activeSlideOffset}
         lockScrollWhileSnapping={true}
         enableSnap={true}
         enableMomentum={false}
         activeSlideOffset={this.props.activeSlideOffset}
         decelerationRate="fast"
        // removeClippedSubviews={true}
         removeClippedSubviews={false}
       />

标签: react-native

解决方案


转到 node_modules/react-native-snap-carousel/src/carousel/Carousel.js 并编辑第 820 行

 if (nextActiveItem === this._itemToSnapTo && scrollOffset === this._scrollOffsetRef) {
    this._repositionScroll(nextActiveItem);
 }

 if (nextActiveItem === this._itemToSnapTo && Math.trunc(scrollOffset) === Math.trunc(this._scrollOffsetRef)) {
    this._repositionScroll(nextActiveItem);
 }

推荐阅读