Last active
April 25, 2018 10:29
-
-
Save yycking/3d6d6753df13e80d5344391269379f08 to your computer and use it in GitHub Desktop.
Carousel in React Native
This file contains hidden or 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
const CardList = (props) => { | |
let snapScroll, lastx, scrollingRight | |
const scrollBack = () => { | |
const len = props.cards.length | |
if (len == 0 || lastx==undefined) { | |
return | |
} | |
let index = scrollingRight? Math.ceil(lastx / cardWith) : Math.floor(lastx / cardWith) | |
if (index < 0) { | |
index = 0 | |
} | |
if (index > len-1) { | |
index = len-1 | |
} | |
props.onChangeIndex(index) | |
const scrollTo = index * cardWith | |
snapScroll.scrollTo({x:scrollTo, y:0 , animated:true}) | |
} | |
return ( | |
<ScrollView | |
ref={(scroll) => { snapScroll = scroll }} | |
style={{ | |
width:cardWith*3, | |
left:0.5*(deviceWidth - cardWith*3), | |
}} | |
decelerationRate={0} | |
onMomentumScrollEnd={Platform.select({ | |
ios: () => {}, | |
android: scrollBack, | |
})} | |
onResponderRelease={Platform.select({ | |
ios: scrollBack, | |
android: () => {}, | |
})} | |
scrollEventThrottle={32} | |
showsHorizontalScrollIndicator={false} | |
horizontal={true} | |
onScroll={(event)=>{ | |
const nextx = event.nativeEvent.contentOffset.x; | |
scrollingRight = (nextx > lastx); | |
lastx = nextx; | |
}} > | |
<View style={{width:cardWith}}/> | |
{ | |
props.cards.map( (obj) => | |
<View | |
key={obj.cardType} | |
style={{ | |
width:cardWith, | |
}} > | |
<Card | |
renderCode={props.renderCode} | |
info={obj} /> | |
</View> | |
) | |
} | |
<View style={{width:cardWith}}/> | |
</ScrollView> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment