React Native Basit Carousel Yapımı için react-native-snap-carousel
kullanacağız. react-native-snap-carousel github adresine göz atabilirsiniz.
Kurulum
npm install --save react-native-snap-carousel
Import
import Carousel from 'react-native-snap-carousel';
Kullanımı
import React from 'react';
import { View, Text, Button, SafeAreaView} from 'react-native';
import Carousel from 'react-native-snap-carousel';
export default class App extends React.Component {
constructor(props){
super(props);
this.state = {
activeIndex:0,
carouselItems: [
{
title:"Item 1",
text: "Text 1",
},
{
title:"Item 2",
text: "Text 2",
},
{
title:"Item 3",
text: "Text 3",
},
{
title:"Item 4",
text: "Text 4",
},
{
title:"Item 5",
text: "Text 5",
},
]
}
}
_renderItem({item,index}){
return (
<View style={{
backgroundColor:'floralwhite',
borderRadius: 5,
height: 250,
padding: 50,
marginLeft: 25,
marginRight: 25, }}>
<Text style={{fontSize: 30}}>{item.title}</Text>
<Text>{item.text}</Text>
</View>
)
}
render() {
return (
<SafeAreaView style={{flex: 1, backgroundColor:'rebeccapurple', paddingTop: 50, }}>
<View style={{ flex: 1, flexDirection:'row', justifyContent: 'center', }}>
<Carousel
layout={"default"}
ref={ref => this.carousel = ref}
data={this.state.carouselItems}
sliderWidth={300}
itemWidth={300}
renderItem={this._renderItem}
onSnapToItem = { index => this.setState({activeIndex:index}) } />
</View>
</SafeAreaView>
);
}
}
Pagination İle Beraber Kullanımı
import React, { Component } from 'react';
import { View, Text, Button, SafeAreaView, Dimensions, Image, StyleSheet } from 'react-native';
import Carousel, { Pagination } from 'react-native-snap-carousel';
//npm install --save-dev react-native-snap-carousel
const SLIDER_WIDTH = Dimensions.get('window').width;
const SLIDER_HEIGHT = Dimensions.get('window').height;
const ITEM_WIDTH = Math.round(SLIDER_WIDTH * 1);
const ITEM_HEIGHT = Math.round(ITEM_WIDTH * 3 / 4);
export default class App extends Component {
state = {
index: 0
}
constructor(props) {
super(props);
this.state = {
activeIndex: 0,
carouselItems: [
{
title: "Item 1",
text: "Text 1",
image: 'slider-19.jpg',
},
{
title: "Item 2",
text: "Text 2",
image: 'slider-18.jpg',
},
{
title: "Item 3",
text: "Text 3",
image: 'slider-17.jpg',
},
{
title: "Item 4",
text: "Text 4",
image: 'slider-16.jpg',
},
]
}
this._renderItem = this._renderItem.bind(this)
}
_renderItem({ item, index }) {
return (
<View style={styles.itemContainer}>
<Image style={{width:ITEM_WIDTH, height:ITEM_HEIGHT}} source={{ uri: `https://siteadi.net/images/slider/${item.image}` }} />
</View>
);
}
get pagination () {
const { carouselItems, activeIndex } = this.state;
return (
<Pagination
dotsLength={carouselItems.length}
activeDotIndex={activeIndex}
containerStyle={{ backgroundColor: 'rgba(0, 0, 0, 0.75)' }}
dotStyle={{
width: 10,
height: 10,
borderRadius: 5,
marginHorizontal: 8,
backgroundColor: 'rgba(255, 255, 255, 0.92)'
}}
inactiveDotStyle={{
// Define styles for inactive dots here
}}
inactiveDotOpacity={0.4}
inactiveDotScale={0.6}
/>
);
}
render() {
return (
<View>
<Carousel
ref={(c) => this.carousel = c}
data={this.state.carouselItems}
renderItem={this._renderItem}
sliderWidth={SLIDER_WIDTH}
itemWidth={ITEM_WIDTH}
containerCustomStyle={styles.carouselContainer}
inactiveSlideShift={0}
onSnapToItem={(index) => this.setState({ activeIndex: index }) }
useScrollView={true}
/>
<Text style={styles.counter}
>
{this.state.index}
</Text>
{ this.pagination }
</View>
);
}
}
const styles = StyleSheet.create({
carouselContainer: {
marginTop: 50
},
itemContainer: {
width: ITEM_WIDTH,
height: ITEM_HEIGHT,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'dodgerblue'
},
itemLabel: {
color: 'white',
fontSize: 24
},
counter: {
marginTop: 25,
fontSize: 30,
fontWeight: 'bold',
textAlign: 'center'
}
});