Created
January 25, 2020 05:53
-
-
Save KBiswas98/46bcdef67e64a333060502cb05c3103e to your computer and use it in GitHub Desktop.
react-native slider/carousel using scrollView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
componentDidMount() { | |
this._stopAutoPlay(); | |
this._startAutoPlay(); | |
} | |
componentWillUnmount() { | |
this._stopAutoPlay(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_goToNextPage = () => { | |
if ( | |
ActivePortionOfScrollView > | |
Dimensions.get('window').width * (NumberOfSlide - 1) | |
) | |
ActivePortionOfScrollView = -Dimensions.get('window').width; | |
ActivePortionOfScrollView = | |
ActivePortionOfScrollView + | |
Dimensions.get('window').width + | |
AdjustingWidth; | |
this.refs.scrollview.scrollTo({ | |
x: ActivePortionOfScrollView, | |
y: 0, | |
animated: true, | |
}); | |
// console.log('ww' + ActivePortionOfScrollView); | |
}; | |
_startAutoPlay = () => { | |
this._timerId = setInterval(this._goToNextPage, IntervalTime); | |
}; | |
_stopAutoPlay = () => { | |
if (this._timerId) { | |
clearInterval(this._timerId); | |
this._timerId = null; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<View style={{marginTop: 10, marginBottom: 10}}> | |
<ScrollView | |
style={{}} | |
horizontal={true} | |
ref="scrollview" | |
onScroll={event => this.handleScroll(event)}> | |
<Image | |
source={require('../../../assets/images/one.jpg')} | |
style={styles.sliderItems} | |
/> | |
<Image | |
source={require('../../../assets/images/two.jpg')} | |
style={styles.sliderItems} | |
/> | |
<Image | |
source={require('../../../assets/images/three.jpg')} | |
style={styles.sliderItems} | |
/> | |
</ScrollView> | |
</View> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://medium.com/@bkamalesh99/react-native-flatlist-as-carousel-slider-ae5ac6497350
https://stackoverflow.com/a/59906574/12003816