欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于React Native 0.52实现轮播图效果

程序员文章站 2023-10-27 23:16:52
本文基于react native 0.52 demo上传到git了,有需要可以看看,写了新内容会上传的(git地址) 一、总览 轮播图几乎是必备的效果,这里选择 re...

本文基于react native 0.52

demo上传到git了,有需要可以看看,写了新内容会上传的(git地址

一、总览

轮播图几乎是必备的效果,这里选择 react-native-swiper 来实现,效果如下图:

基于React Native 0.52实现轮播图效果

二、实现轮播图效果

1、通过npm安装react-native-swiper

npm install react-native-swiper --save

2、在recommend.js引入react-native-swiper

import swiper from 'react-native-swiper';

3、用 react-native-swiper 可以很容易的实现轮播的效果

  • showbuttons —— 是否显示左右翻页按钮
  • autoplay —— 是否自动播放
  • paginationstyle —— 包含小点点的容器的样式,这里用来调整位置
  • dotstyle —— 小点点的样式
  • activedotstyle —— 当前被激活的小点点的样式
<swiper
   style={styles.wrapper}
   height={width*40/75}
   showsbuttons={false}
   autoplay={true}
   paginationstyle={styles.paginationstyle}
   dotstyle={styles.dotstyle}
   activedotstyle={styles.activedotstyle}
>
    <image source={require('../../img/1.jpg')} style={styles.bannerimg} />
    <image source={require('../../img/2.jpg')} style={styles.bannerimg} />
    <image source={require('../../img/3.jpg')} style={styles.bannerimg} />
    <image source={require('../../img/4.jpg')} style={styles.bannerimg} />
    <image source={require('../../img/3.jpg')} style={styles.bannerimg} />
</swiper>

样式:

const styles = stylesheet.create({
  container: {
    flex: 1,
    alignitems: 'center',
    backgroundcolor: '#fff',
  },
  bannerimg: {
    height: width*40/75,
    width: width,
  },
  wrapper: {
    width: width,
  },
  paginationstyle: {
    bottom: 6,
  },
  dotstyle: {
    width: 22,
    height: 3,
    backgroundcolor:'#fff',
    opacity: 0.4,
    borderradius: 0,
  },
  activedotstyle: {
    width: 22,
    height: 3,
    backgroundcolor:'#fff',
    borderradius: 0,
  },
});

三、解决不显示问题

轮播图放在app的首页,同样有不显示的问题,解决办法和上一篇的办法几乎一样,可以看一下上一篇或是完整代码,这里就不再赘述。

这里和上一篇相比有两处不一样,需要说一下。

1、真正调用接口加载图片的时候,不会出现一开始图片不显示的问题。

2、在状态为false的时候,先显示第一张图片

if (this.state.swipershow) {
  return (
   <swiper >
     …………略
   </swiper>
  )
} else {
  return (
   <view style={{ height: width*40/75 }}>
     <image source={require('../../img/1.jpg')} style={styles.bannerimg} />
   </view>
  );
}

recommend.js完整代码 地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。