【React Native 安卓开发】----第三方框架的引用之React-native-Swiper框架实现欢迎页【第五篇】
来源:互联网 发布:淘宝商城女棉衣 编辑:程序博客网 时间:2024/06/05 07:29
前言
今天要介绍的是React-native-Swiper这个RN开源框架,如果你不想用第三方的你也可以自己用viewPagerAndroid去实现,这里先不做介绍了,很简单。
想要了解React-native-Swiper源码的童鞋可以在github直接搜索React-native-Swiper。
对于安卓同学来说应该都用过ViewPagerIndicator 做引导页或者导航栏,这里就不多说了,今天要讲的React-native-Swiper也是一款非常叼的开源框架,接下来然我们一起来看一看。
引入依赖和模块
1.首先进入到工程目录根目录下面 即:package.json所在的目录。
(package.json里面有工程信息及所有依赖,相当于安卓的gradle,然后我们所依赖的那些库在node-modules里面,就相当于安卓之前的libs文件夹)
2.通过npm安装模块
npm i react-native-swiper –save
成功后会有如下显示:
这个意思就是安装这个模块到node-modules,同时配置到package.json里面去。
– save的作用就是配置package.json。
如下:会在package.json依赖里面多了一个react_native-swiper:”^1.5.4”
同时让我们观察一下node-modules文件夹:
多了react-native-swiper文件
3.几个常用命令便于管理工程
查看模块:npm view react-native-swiper
删除模块:npm rm react-native-swiper –save (这个添加save会在删除的同时去除package.json中的依赖)
查看帮助命令:npm help 命令 (例如npm help -i查看i的使用)
使用效果
先来看一下效果:
下面是代码:可以直接自己coyp自己体验一下,切记AppRegistry.registerComponent('hello', () => hello);
需要自己改一下
import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, Image, TouchableOpacity, ViewPagerAndroid, Navigator, View } from 'react-native';import Swiper from 'react-native-swiper';class hello extends Component { render() { return ( <Swiper style={styles.wrapper} showsButtons={true}> <View style={styles.slide1}> <Text style={styles.text}>Hello Swiper</Text> </View> <View style={styles.slide2}> <Text style={styles.text}>Beautiful</Text> </View> <View style={styles.slide3}> <Text style={styles.text}>And simple</Text> </View> </Swiper> ); }}const styles = StyleSheet.create({ wrapper: { }, slide1: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#9DD6EB', }, slide2: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#97CAE5', }, slide3: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#92BBD9', }, text: { color: '#fff', fontSize: 30, fontWeight: 'bold', }});AppRegistry.registerComponent('hello', () => hello);
详细属性
接下来让我们好好探索一下这个框架的基本属性:
基本属性
下面演示一下下面这些样式的效果 我设置默认选择第二页,显示button,小圆点在最下面,禁用无限循环。<Swiper style={styles.wrapper} showsButtons={true} horizontal={true} loop={false} index={1}> <View style={styles.slide1}> <Text style={styles.text}>我是第一页</Text> </View> <View style={styles.slide2}> <Text style={styles.text}>我是第二页</Text> </View> <View style={styles.slide3}> <Text style={styles.text}>我是第三页</Text> </View> </Swiper>
显示效果如下:
自定义基本样式
接来下体验下一下效果
设置宽高为200,200,loadMinimal为true加载当前索引幻灯片。
<Swiper style={styles.wrapper} showsButtons={true} horizontal={true} loop={false} index={1} loadMinimal={true}>
效果图:
可以看出宽高都有了变化 而且只加载了一个
视图,其他的都是空白的
当我们把loadMinimal设置为true同时,loadMinimalSize设置为3这时候就回复正常了,让我们看一下效果:
<Swiper style={styles.wrapper} showsButtons={true} horizontal={true} loop={false} index={1} loadMinimal={true} loadMinimalSize={3} >
Pagination分页
接下来让我们看一个分页的demo:
先看一下效果:
/** * Sample React Native App * https://github.com/facebook/react-native */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, Image, TouchableOpacity, ViewPagerAndroid, Navigator, View, Dimensions } from 'react-native';import Swiper from 'react-native-swiper';const { width } = Dimensions.get('window')class hello extends Component { render() { return ( <View> <Swiper style={styles.wrapper} height={200} horizontal={true} autoplay={true}> <View style={styles.slide1}> <Text style={styles.text}>第一页</Text> </View> <View style={styles.slide2}> <Text style={styles.text}>第二页</Text> </View> <View style={styles.slide3}> <Text style={styles.text}>第三页</Text> </View> </Swiper> <Swiper style={styles.wrapper} height={240} dot={<View style={{backgroundColor: 'rgba(0,0,0,.2)', width: 5, height: 5, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3}} />} activeDot={<View style={{backgroundColor: '#000', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3}} />} paginationStyle={{ bottom: -23, left: null, right: 10 }} loop> <View style={styles.slide} title={<Text numberOfLines={1}>Aussie tourist dies at Bali hotel</Text>}> <Image resizeMode='stretch' style={styles.image} source={require('./imgs/1.jpg')} /> </View> <View style={styles.slide} title={<Text numberOfLines={1}>Big lie behind Nine’s new show</Text>}> <Image resizeMode='stretch' style={styles.image} source={require('./imgs/2.jpg')} /> </View> <View style={styles.slide} title={<Text numberOfLines={1}>Why Stone split from Garfield</Text>}> <Image resizeMode='stretch' style={styles.image} source={require('./imgs/3.jpg')} /> </View> <View style={styles.slide} title={<Text numberOfLines={1}>Learn from Kim K to land that job</Text>}> <Image resizeMode='stretch' style={styles.image} source={require('./imgs/4.jpg')} /> </View> </Swiper> </View> ); }}const styles = StyleSheet.create({ wrapper: { }, slide: { flex: 1, justifyContent: 'center', backgroundColor: 'transparent' }, slide1: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#9DD6EB' }, slide2: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#97CAE5' }, slide3: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#92BBD9' }, text: { color: '#fff', fontSize: 30, fontWeight: 'bold' }, image: { width, flex: 1 }});AppRegistry.registerComponent('hello', () => hello);
Autoplay自动换图
还以上个例子讲解 这里设置 autoplayDirection为false ;autoplayTimeout为1;
效果如图:
代码如下:
<Swiper style={styles.wrapper} height={200} horizontal={true} autoplay={true} autoplayTimeout={1} autoplayDirection={false}>
Control buttons自定义按钮
在上一个demo的基础上进行衍生demo:
先看效果图:
再看样式代码:
<Swiper style={styles.wrapper} height={200} horizontal={true} autoplay={true} autoplayTimeout={1} autoplayDirection={false} showsButtons={true}nextButton={<Text style={styles.buttonText}>》》</Text>}>
- 【React Native 安卓开发】----第三方框架的引用之React-native-Swiper框架实现欢迎页【第五篇】
- 【React Native 安卓开发】----侧边栏的实现DrawerLayoutAndroid以及第三方框架react-native-side-menu的使用【第六篇】
- React-Native学习--轮播图第三方组件-react-native-swiper
- React-Native学习--轮播图第三方组件-react-native-swiper
- react native 怎么引用第三方库
- 将第三方组件react-native-swiper应用在欢迎界面中
- react-native样式的框架
- react -native 优秀的框架
- React/React Native框架的设计思想
- React Native开发之常用第三方控件
- React Native开发之常用第三方控件
- RN-第三方之-react-native-fs
- 【React Native 安卓开发】----(View实战之仿携程)【第三篇】
- React Native框架介绍
- react native 常用的第三方工具包
- React-native 第三方组件
- React-Native-swiper React Native 轮播类组件
- React Native 如何做轮播图 react-native-swiper
- IOS移动开发 应用程序互相切换问题
- Cookie和Session机制
- 转 C# 文件下载四方法
- x86 kernel 中断机制分析二——irq_desc
- 2016网络程序设计学习心得-李鹏程-SA16225146
- 【React Native 安卓开发】----第三方框架的引用之React-native-Swiper框架实现欢迎页【第五篇】
- 【javascript 技巧】谈谈setTimeout的作用域以及this的指向问题
- Linux下history命令用法
- ISO swift语言 图片抖动
- Map的各种用法
- eclipse导入Spring框架
- BZOJ2085: [Poi2010]Hamsters
- Xcode4.6.3中的iphone simulator6.1太大怎么改
- Android IPC机制(五)用Socket实现跨进程聊天程序