0

Trying to implement a image slider in React native using scrollview. The scrollview should scroll back to the first image if the user scrolls right from the last image. Implementing as a functional component. How do i detect that the user has scrolled ? I have tried using onScroll method of ScrollView , but it fires too many events. I want to basically reset the scrollview to first image if the user scrolls past the last image.

const scrollRef = useRef()

<View style={styles.container}>
      <ScrollView
       ref={ref => { scrollRef.current = ref }}
       horizontal={true} 
       showsHorizontalScrollIndicator={false}
       >

       {images.map((image, imageId) => {
              return (
                <TouchableHighlight
                  key={imageId}
                  underlayColor='red'
                >
                <Image source={{uri: image}} style={{width:width, aspectRatio: 3, resizeMode: 'cover'}} />
              </TouchableHighlight>
              )
        })}
      </ScrollView>
   </View>

1 Answer 1

1

Please use Flatlist instead of scrollview,

        <FlatList
          ref={(ref) => {
            flatListRef = ref;
          }}
          horizontal={true}
          pagingEnabled={true}
          data={imageArray} //files
          legacyImplementation={true}
          showsHorizontalScrollIndicator={false}
          renderItem={({ item, index }: any) =>
            (
              <View
                style={{
                  width: width,
                  height: '100%',
                  justifyContent: 'center',
                  // alignItems: 'center',
                  backgroundColor: 'white'
                }}
              >

                <Image source={{ uri: item.uri }} style={{ width: '100%', 
                 height: item.height }}></Image>
                <Text style={{
                  paddingTop: 14, paddingLeft: 20, fontFamily: 'Roboto- 
                  Regular', fontSize: 16, color: 'rgb(74,74,74)'
                }}>{description + " " + item.width + 'x' + item.height + 
              '.jpg'}</Text>
              </View>
            )}
        />```

please try this code, hope this works for you
Sign up to request clarification or add additional context in comments.

5 Comments

My scrollview is working. I want to make the scroll position reach back to 1 in case of if the user scrolls past last image. How do I do that in Flatlist ?
In the flast list you need to create Ref and using that ref you can use the flatlist method this.flatListRef.scrollToIndex({ index: this.state.currentindex, animated: true }); Also I have update the above code. You can see how i had assign the Ref.
On what event should the index be updated ? I tried doing it on onScroll, however onScroll is not reliable and gets triggered multiple times.
@PokemonPlayer Please try above code I have implemented this code at my end. It's working for me. Hope it will also work for you. If not work then please do let me know.
This just gives a basic carousel. If i have 4 images, it does not scroll back to first position if i try to scroll past the last image, which is the problem i am trying to solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.