刚接触React-Native不久,我就被深深折服了。

前几天做项目用到了ViewPager做广告页,在研究了一番官方文档之后,终于也是大体做出来了,今天就分享给大家吧。

其实,大家如果使用过Android的ViewPager,那么你可能会很容易的去理解这个组件了。

下面是实现的代码(涉及到ViewPager实现的我会标出):

 use strict';

 var React = require('react-native');
var FirstPage = require('./firstPage');
var SecondPage = require('./secondPage');
var ThirdPage = require('./thirdPage');
var ForthPage = require('./forthPage');
var FifthPage = require('./fifthPage');
var NavBar = require('rn-navbar'); var Dimensions = require('Dimensions'); var {
Image,
StyleSheet,
Text,
TouchableWithoutFeedback,
TouchableOpacity,
View,
ViewPagerAndroid,
Navigator,
TouchableOpacity,
} = React; var PAGES = 5;
var BGCOLOR = ['#fdc08e', '#fff6b9', '#99d1b7', '#dde5fe'];//
var deviceWidth = Dimensions.get('window').width;
var deviceHeight = Dimensions.get('window').height; var ViewPagerAndroidExample = React.createClass({
getInitialState: function() {//初始化viewpager
return {
page: 0,
animationsAreEnabled: true,
progress: {
position: 0,
offset: 0,
},
};
},
onPageSelected: function(e) {//当页面选中的时候
this.setState({page: e.nativeEvent.position});
},
//滑动
onPageScroll: function(e) {
this.setState({progress: e.nativeEvent});
},
//移动到某一页
move: function(delta) {
var page = this.state.page + delta;
this.go(page);
},
//跳到某一页
go: function(page) {
if (this.state.animationsAreEnabled) {
this.viewPager.setPage(page);
} else {
this.viewPager.setPageWithoutAnimation(page);
}
this.setState({page});
},
//根据页数加载界面
render: function() {
var pages = [];
for (var i = 0; i < PAGES; i++) {
var pageStyle = {
backgroundColor: BGCOLOR[i % BGCOLOR.length],
alignItems: 'center',
justifyContent : 'center',
};
if(i === 0){
pages.push(
<View key={i} style={pageStyle} collapsable={false}>
<FirstPage />
</View>
);
}else if(i === 1){
pages.push(
<View key={i} style={pageStyle} collapsable={false}>
<SecondPage />
</View>
);
}
else if(i === 2){
pages.push(
<View key={i} style={pageStyle} collapsable={false}>
<ThirdPage />
</View>
);
}else if(i === 3){
pages.push(
<View key={i} style={pageStyle} collapsable={false}>
<ForthPage />
</View>
);
}else if(i === 4){
pages.push(
<View key={i} style={pageStyle} collapsable={false}>
<FifthPage />
</View>
);
}
else{
pages.push(
<View key={i} style={pageStyle} collapsable={false}>
</View>
);
}
}
var { page, animationsAreEnabled } = this.state; return (
<View style={styles.container} >
<NavBar
navigator={this.props.navigator}
title="关于"
renderScene={this.renderScene}
backFunc={()=>{this.props.navigator.pop()}}/>
<View style = { styles.vpContainer }>
<ViewPagerAndroid
style={styles.viewPager}
initialPage={0}
onPageScroll={this.onPageScroll}
onPageSelected={this.onPageSelected}
ref={viewPager => { this.viewPager = viewPager; }}>
{pages}
</ViewPagerAndroid>
</View>
<View style = { [styles.dotView1,{ opacity : page == 0 ? 1 : 0.4 }] } />
<View style = { [styles.dotView2 , { opacity : page == 1 ? 1 : 0.4 }] }/>
<View style = { [styles.dotView3, { opacity : page == 2 ? 1 : 0.4 }] } />
<View style = { [styles.dotView4, { opacity : page == 3 ? 1 : 0.4 }] } />
<View style = { [styles.dotView5, { opacity : page == 4 ? 1 : 0.4 }] } />
</View>
);
},
}); var NavigationBarRouteMapper = {
LeftButton(route, navigator, index, nextState) {
return (
<TouchableOpacity style={{flex:1}}
onPress={() => navigator.parentNavigator.pop()}>
<View style = {styles.titleBackView} >
<Image source = {require('../imgs/icon_back.png')} style = { styles.titleBackimg }/>
<Text style={ styles.titleBackText }>
返回
</Text>
</View>
</TouchableOpacity>
);
},
RightButton(route, navigator, index, nextState) {
return (
<View/>
);
},
Title(route, navigator, index, nextState) {
return (
<View style = { styles.titleCenterView } >
<Text style={ styles.titleCenterText } >
关于
</Text>
</View>
);
}
}; //dotview小圆点,用于表示滑动到哪一页圆点
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
vpContainer: {
flex: 9,
},
viewPager: {
flex: 1,
},
dotView1: {
position : 'absolute' ,
width : deviceWidth/30 ,
height : deviceWidth/30,
borderRadius: deviceWidth/60,
top : deviceHeight-deviceHeight/8,
right : deviceWidth / 2 + 2*deviceWidth/15 ,
backgroundColor : 'gray' ,
opacity : 0.4 ,
flexDirection : 'column',
},
dotView2: {
position : 'absolute' ,
width : deviceWidth/30 ,
height : deviceWidth/30,
top : deviceHeight-deviceHeight/8 ,
right : deviceWidth / 2 + deviceWidth/15 ,
backgroundColor : 'gray' ,
borderRadius: deviceWidth/60,
opacity : 0.4 ,
flexDirection : 'column',
},
dotView3: {
position : 'absolute' ,
width : deviceWidth/30 ,
height : deviceWidth/30,
top : deviceHeight-deviceHeight/8,
right : deviceWidth / 2 ,
backgroundColor : 'gray' ,
borderRadius: deviceWidth/60,
opacity : 0.4 ,
flexDirection : 'column',
},
dotView4: {
position : 'absolute' ,
width : deviceWidth/30 ,
height : deviceWidth/30,
borderRadius: deviceWidth/60,
top : deviceHeight-deviceHeight/8,
right : deviceWidth / 2 - deviceWidth/15 ,
opacity : 0.4 ,
backgroundColor : 'gray' ,
flexDirection : 'column',
},
dotView5: {
position : 'absolute' ,
width : deviceWidth/30 ,
height : deviceWidth/30,
borderRadius: deviceWidth/60,
top : deviceHeight-deviceHeight/8,
right : deviceWidth / 2 - 2*deviceWidth/15 ,
opacity : 0.4 ,
backgroundColor : 'gray' ,
flexDirection : 'column',
},
dotImg : {
flex : 1 ,
},
titleBackView : {
flex : 1 ,
flexDirection : 'row',
justifyContent: 'center',
alignItems : 'center', },
titleBackimg : {
flex:1,
resizeMode : 'contain',
height: 40,
width: 40,
marginTop : deviceHeight/60,
},
titleBackText : {
color: '#53CEFF' ,
fontSize : deviceHeight/50,
marginTop : deviceHeight/60,
},
titleCenter : {
flex: 5,
height:70,
width: 0 ,
alignItems: 'center',
},
titleCenterText :{
color: 'black',
fontSize: deviceHeight/30,
alignSelf : 'center',
textAlign : 'center',
},
titleCenterView : {
flexDirection : 'row',
justifyContent: 'center',
alignItems : 'center',
position : 'absolute',
right: deviceWidth / 2 - deviceHeight/30/2,
top : 0,
backgroundColor : 'green',
},
}); module.exports = ViewPagerAndroidExample;

React-Native之ViewPagerAndroid的使用的更多相关文章

  1. React Native之ViewPagerAndroid跳转页面问题

    前言: 网上目前react-native的教程较少,加上许多帖子还是用的ES5(2015年6月已发布ES6标准),有些细节很难找到答案,这里把遇到的问题做一个分享,让学习者尽量少踩坑. 出现问题: 1 ...

  2. React Native之ViewPagerAndroid 组件

    概述 今天我们来讲解一下关于 ViewPager 的使用,它是一个允许子视图左右滚动翻页的容器.我们知道在Android开发中系统有ViewPager这个组件,作用是实现滚动翻页的,在RN中也是有这么 ...

  3. 【转】【React Native开发】

    [React Native开发]React Native控件之ListView组件讲解以及最齐全实例(19)  [React Native开发]React Native控件之Touchable*系列组 ...

  4. React Native专题

    转载注明出处:地址:http://www.lcode.org本文出自:[江清清的技术专栏]本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶 ...

  5. react native 第三方组件react-native-swiper 轮播组件

    github地址:https://github.com/leecade/react-native-swiper 使用方法:安装:npm i react-native-swiper –save 查看模块 ...

  6. React Native专题-江清清

    本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶相关讲解. 刚创建的React Native交流8群:533435865  欢迎各位大牛, ...

  7. React Native 常用插件案例

    (二).基础入门: 1.React Native For Android环境配置以及第一个实例 2.React Native开发IDE安装及配置 3.React Native应用设备运行(Runnin ...

  8. [RN] React Native 实现图片预览

    [RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...

  9. [RN] React Native中使用 react-native-scrollable-tab-view嵌套在ScrollView里,导致 子内容 在安卓上无法显示

    React Native中使用 react-native-scrollable-tab-view嵌套在ScrollView里,导致 子内容 在安卓上无法显示 问题: 0.9.0 或 0.8.0 版本的 ...

  10. React Native 之 Text的使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

随机推荐

  1. django Model模型二及Model模型对数据库的操作

    在django模型中负责与数据库交互的为Model层,Model层提供了一个基于orm的交互框架 一:创建一个最基本的Model from __future__ import unicode_lite ...

  2. 空合并操作符??(C#)

    ??二元操作符在对first??second求值时,大致会经历以下步骤: 1)对first进行求值: 2)如果结果非空,则该结果就是整个表达式的结果: 3)否则求second的值,其结果作为整个表达式 ...

  3. I/O多路复用之select

    1.什么是I/O多路复用 关于什么是I/O多路复用,在知乎上有个很好的回答,可以参考罗志宇前辈的回答. 这里记录一下自己的理解.我认为要理解这个术语得从两方面去出发,一是:多路是个什么概念?二是:复用 ...

  4. c++连接mysql数据库(使用mysql api方式,环境VS2013+MYSQL5.6)

    转载请注明出处,原文地址http://www.cnblogs.com/zenki-kong/p/4382657.html 刚开始写博客,博主还只是个大三汪,学艺不精,如有错误还请前辈指出(>^ω ...

  5. discuz x2 个人资料项排序问题解决方法、添加自定义字段、修改栏目名称和介绍

    第一次写文章,希望与人提供方便同时,别误人子弟,自己研究的,大家看不懂只改文件就可以了,如果发现不对的地方请回复或直接通知我,谢谢,本来想在discuz论坛上发的,不懂版规也没时间看版规,怕发错,隔小 ...

  6. 【Lucene4.8教程之五】Luke

    一.Luke基本内容 1.Luke简介 Luke可用于查看Lucene创建的索引,并对其进行基本操作. 2.创建Luke (1)从Github上下载源文件 https://github.com/tar ...

  7. float的理解

    1.浮动包裹性——浮动具有让元素按displya:inline-block显示(如果没有设置宽度和高度,则它可以显示的尽量窄高 度尽量小).2.浮动破坏性——浮动元素漂浮在标准流之上(但没有脱离文档流 ...

  8. [C++程序设计]指针总结

  9. win 下python2.7 pymssql连接ms sqlserver 2000

    python DB-API 连接mysql 要用到库pymssql 下载请到https://pypi.python.org/pypi/pymssql/2.0.1我这里下载的是ms windows in ...

  10. 闲聊之Python的数据类型 - 零基础入门学习Python005

    闲聊之Python的数据类型 让编程改变世界 Change the world by program Python的数据类型 闲聊之Python的数据类型所谓闲聊,goosip,就是屁大点事可以咱聊上 ...