首页 > 其他 > 详细

react-native-snap-carousel 轮播

时间:2020-02-26 23:14:38      阅读:287      评论:0      收藏:0      [点我收藏+]

效果如下:

 
技术分享图片
6iIb4SR.gif

使用插件: https://www.npmjs.com/package/react-native-snap-carousel

基础使用方法:

 下载:  $ npm install --save react-native-snap-carousel
 引入:  import Carousel from react-native-snap-carousel

 使用:
const horizontalMargin = 20;
const slideWidth = 280;
const itemWidth = slideWidth + horizontalMargin * 2;
const itemHeight = 200;
  export class MyCarousel extends Component {
      constructor(props) {
            super(props);
            this.state = {
                entries: [
                    { title: "安徒生童话" },
                    { title: "格林童话" },
                    { title: "我的童话"}
                ]
            }
       }


    _renderItem ({item, index}) {
        return (
            <View style={styles.slide}>
                <Text style={styles.title}>{ item.title }</Text>
            </View>
        );
    }
 
    render () {
        return (
            <Carousel
              ref={(c) => { this._carousel = c; }}
              data={this.state.entries}
              renderItem={this._renderItem}
              sliderWidth={sliderWidth}
              itemWidth={itemWidth}
            />
        );
    }
}


const styles = StyleSheet.create({ 
    slide: {
      borderRadius: 10
      backgroundColor: #992211
   }
})


常用参数


                 <Carousel 
                    ref={(c) => { this._carousel = c; }}  // 获取节点
                    inactiveSlideOpacity={1} // 不活动幻灯片的不透明度效果的值
                    inactiveSlideScale={0.7}  // 不活动幻灯片的缩放效果的值
                    firstItem={0} // 第一个显示Item
                    removeClippedSubviews={false} // 解决 IOS如果不滑动就不显示的额问题
                    data={this.state.cardList}
                    renderItem={this._renderItem}
                    sliderWidth={sliderWidth} // 轮播的宽度
                    itemWidth={itemWidth} //  单个Item的宽度
                    onSnapToItem={()=>{this.onSnapToItem()}}   //导航到项目时触发回
                    />
常用方法


获取当前显示Item的Index
const carouselCurrIndex = this._carousel.currentIndex;

跳转到指定Item

this._carousel.snapToItem(2) // 显示第二个item

 

react-native-snap-carousel 轮播

原文:https://www.cnblogs.com/plBlog/p/12369764.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!