meliorence / react-native-snap-carousel

Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.
BSD 3-Clause "New" or "Revised" License
10.27k stars 2.27k forks source link

onSnapToItem never gets called #541

Open mohammedmunaf opened 5 years ago

mohammedmunaf commented 5 years ago

Using "react-native-snap-carousel": "3.7.5" Testing on iOS

Here is my code

<Carousel
                    ref={(c) => { this._carousel = c; }}
                    vertical={true}
                    data={["1", "2", "3", "4", "5", "6", "7", "8"]}
                    renderItem={({ item, index }) =>

                        <TouchableOpacity
                            style={{ flex: 1, backgroundColor: "transparent" }}
                            onPress={() => { this._carousel.snapToItem(index) }}>
                            <Text
                                style={{
                                    height: 30,
                                    textAlignVertical: 'center',
                                    flex: 1,
                                    textAlign: 'center',
                                    fontSize: 16
                                }} key={"money" + item}>{item}</Text>
                        </TouchableOpacity>
                    }
                    onSnapToItem={(index) => {
                        console.log("Moved to=====> " + index)
                        alert(index)
                    }}
                    inactiveSlideOpacity={0.4}
                    inactiveSlideScale={0.9}
                    enableSnap={true}
                    removeClippedSubviews={false}
                    autoplay={false}
                    sliderHeight={80}
                    sliderWidth={90}
                    itemHeight={30}
                    itemWidth={90}
                />
adrian1388 commented 5 years ago

I am facing something similar in Desktop, but the difference is that on Android works OK. I have not tested on iOS yet.

Using "react-native-snap-carousel": "3.8.0"

Code:

...
changeTitle(index) {
  this.setState({
    slider1ActiveSlide: index
  });
}
...
          <Carousel
            ref={c => {
              this._carousel = c;
            }}
            data={data}
            layout="default"
            renderItem={this._renderItem}
            sliderWidth={viewportWidth}
            itemWidth={viewportWidth}
            onSnapToItem={index => this.changeTitle(index)}
            useScrollView
          />
adrian1388 commented 5 years ago

I am facing something similar in Desktop, but the difference is that on Android works OK. I have not tested on iOS yet.

Using "react-native-snap-carousel": "3.8.0"

Code:

...
changeTitle(index) {
  this.setState({
    slider1ActiveSlide: index
  });
}
...
          <Carousel
            ref={c => {
              this._carousel = c;
            }}
            data={data}
            layout="default"
            renderItem={this._renderItem}
            sliderWidth={viewportWidth}
            itemWidth={viewportWidth}
            onSnapToItem={index => this.changeTitle(index)}
            useScrollView
          />

I found that this is the issue for me...

mohammedmunaf commented 5 years ago

Hi folks, Seems very cosmetic bug. Setting activeSlideOffset={2} solved my issue

cesar3030 commented 5 years ago

I'm having the same issue and activeSlideOffset={2} didn't solve it. @adrian1388 Did you figure out a way to fix this?

mohammedmunaf commented 5 years ago

@cesar3030 I was facing this issue while I scroll fast on Android. I resolved it by adding

activeSlideOffset={2}
enableMomentum={true}
decelerationRate={0.9}

Here is my final code

<Carousel
                    ref={(c) => { this._carousel = c; }}
                    layout={'default'}
                    vertical={true}
                    data={this.props.data ? this.props.data : []}
                    renderItem={this.renderItem.bind(this)}
                    onSnapToItem={this._onSnapToItem.bind(this)}
                    sliderHeight={this.props.sliderHeight}
                    inactiveSlideOpacity={0.4}
                    inactiveSlideScale={0.9}
                    activeSlideOffset={2}
                    lockScrollWhileSnapping={true}
                    enableSnap={true}
                    enableMomentum={true}
                    decelerationRate={0.9}
                    removeClippedSubviews={false}
                    autoplay={false}
                    itemHeight={this.props.itemHeight}
                    initialNumToRender={this.props.data ? this.props.data.length : 0}
                    firstItem={0}
                />
cesar3030 commented 5 years ago

@mohammedmunaf Thank you for your help! My problem came from the fact I was using the carousel in a bottom sheet component from the react-native-reanimated-bottom-sheet library. On Android the touch event was not captured because of the bottom sheet.

jrobber commented 4 years ago

@mohammedmunaf Thank you for your help! My problem came from the fact I was using the carousel in a bottom sheet component from the react-native-reanimated-bottom-sheet library. On Android the touch event was not captured because of the bottom sheet.

I'm having the same error. What was the fix?

munafmohammed commented 4 years ago

@mohammedmunaf Thank you for your help! My problem came from the fact I was using the carousel in a bottom sheet component from the react-native-reanimated-bottom-sheet library. On Android the touch event was not captured because of the bottom sheet.

I'm having the same error. What was the fix?

https://github.com/archriss/react-native-snap-carousel/issues/541#issuecomment-514165668

dohooo commented 2 years ago

Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2

tkud04 commented 1 year ago

Same issue today, please any suggestions on how to fix would be appreciated