刚接触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. ASP.NET导入Excel到SQL数据库

    protected void btnChange_Click(object sender, EventArgs e) { UserInfoClass tClass = (UserInfoClass)S ...

  2. 数据结构c++语言描述——最大堆(MaxHeap)

    一.最大堆的插入 图9-3a 给出了一个具有5个元素的最大堆.由于堆是完全二叉树,当加入一个元素形成6元素堆时,其结构必如9-3b 所示.如果插入元素的值为1,则插入后该元素成为2的左孩子,相反,若新 ...

  3. Linq 标准查询操作符三

    本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...

  4. MFC串口通信

    1.串口的操作可以有两种操作方式:同步操作方式和重叠操作方式(又称为异步操作方式). 同步操作时,API函数会阻塞直到操作完成以后才能返回(在多线程方式中,虽然不会阻塞主线程,但是仍然会阻塞监听线程) ...

  5. hdu1443(约瑟夫环游戏的原理 用链表过的)

    Problem Description The Joseph's problem is notoriously known. For those who are not familiar with t ...

  6. 问题汇总-20130927-关于rc.local命令无法执行

    场景:/etc/rc.local有语句 /usr/local/apache/bin/apachectl start mysql cactidb -u root -p123456 -e ' set gl ...

  7. 关于bootstrap列偏移的两种方式

    第一种方式: <div class="col-md-2 col-md-offset-9"> <input type="text" class= ...

  8. py2exe 生成带图标的单个文件实例

    随便拉了个学习时用的测试程序来做的实例,原程序如下: #Filename:for.py count=0 for i in range(1,100,2): count+=i else: print 't ...

  9. HQL(Hibernate Query language)语言

    现在有两张表:student(学生表),classroom(教室表). //对象 Student 对应 student 表中有四个字段,分别是:id,name,age,classroom; publi ...

  10. H5+Boostrap的音乐播放器

    H5+Boostrap做简单的音乐播放器 前言:这个是综合一下我最近在学的东西做的小Demo,到实际使用还有距离,但是用来练手巩固知识点还是不错的,最近在二刷JS书和Boostrap.css的源码,做 ...