基于React Native构建的仿京东客户端(二) - 实现轮播图

前端之家收集整理的这篇文章主要介绍了基于React Native构建的仿京东客户端(二) - 实现轮播图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

安装轮播插件:@H_404_2@

myths-Mac:JdApp myth$ yarn add react-native-swiper

App.js文件完整的代码如下:

import React,{ Component } from 'react';@H_404_2@import {@H_404_2@ StyleSheet,@H_404_2@ Text,@H_404_2@ View@H_404_2@} from 'react-native';@H_404_2@import Header from './Header';@H_404_2@import HomePage from './home/HomePage';@H_404_2@@H_404_2@export default class App extends Component {@H_404_2@ render() {@H_404_2@ return (@H_404_2@ <View style={styles.container}>@H_404_2@ <Header/>@H_404_2@ <HomePage/>@H_404_2@ </View>@H_404_2@ );@H_404_2@ }@H_404_2@}@H_404_2@@H_404_2@const styles = StyleSheet.create({@H_404_2@ container: {@H_404_2@ flex: 1,@H_404_2@ },@H_404_2@

});

@H_404_2@

HomePage.js文件完整的代码如下:

import React,@H_404_2@ View,@H_404_2@ Image,@H_404_2@ Dimensions,@H_404_2@ ScrollView,@H_404_2@ ListView@H_404_2@} from 'react-native';@H_404_2@import Swiper from 'react-native-swiper';@H_404_2@const {width} = Dimensions.get('window');@H_404_2@export default class HomePage extends Component {@H_404_2@ constructor(props){@H_404_2@ super(props)@H_404_2@ this.state = {@H_404_2@ apiData: [@H_404_2@ {id:1,swiperText:'499¥ 限量抢购真八核 腾讯雷霆手机',imageUri: 'http://localhost:8081/images/banner/1.jpg'},@H_404_2@ {id:2,swiperText:'购趣洗手液 赢迪士尼之旅 满88元减22元 满188元减66元',imageUri: 'http://localhost:8081/images/banner/2.jpg'},@H_404_2@ {id:3,swiperText:'潮流焕新季 炫品抄底 3月大促销第三波 时间3月16日~22日',imageUri: 'http://localhost:8081/images/banner/3.jpg'},@H_404_2@ {id:4,swiperText:'三月女人节 春装新品 折后满减 倒数计时',imageUri: 'http://localhost:8081/images/banner/4.jpg'}@H_404_2@ ]}@H_404_2@ this.id = null;@H_404_2@ this.swiperText = null;@H_404_2@ this.imageUri = null;@H_404_2@ }@H_404_2@ render() {@H_404_2@ const data = this.state.apiData;@H_404_2@ let dataDisplay = data.map(function(jsonData){@H_404_2@ return (@H_404_2@ <View key={jsonData.id} style={styles.slide} >@H_404_2@ <Text numberOfLines={1}>{jsonData.swiperText}</Text>@H_404_2@ <Image resizeMode='stretch' style={styles.image} source={{uri: jsonData.imageUri}} />@H_404_2@ </View>@H_404_2@ )@H_404_2@ });@H_404_2@ return(@H_404_2@ <View style={{flex: 1}}>@H_404_2@ <ScrollView style={styles.container}>@H_404_2@ <Swiper style={styles.wrapper} height={140} autoplay@H_404_2@ onMomentumScrollEnd={(e,state,context) => console.log('index:',state.index)}@H_404_2@ dot={<View style={{backgroundColor:'rgba(0,.3)',width: 6,height: 6,borderRadius: 3,margin: 5 }} />}@H_404_2@ activeDot={<View style={{backgroundColor:'red',margin: 5 }} />}@H_404_2@ paginationStyle={{bottom: 0}} loop>@H_404_2@ {dataDisplay}@H_404_2@ </Swiper>@H_404_2@ </ScrollView>@H_404_2@ </View>@H_404_2@ );@H_404_2@ }@H_404_2@}@H_404_2@@H_404_2@const styles = StyleSheet.create({@H_404_2@ container: {@H_404_2@ flex: 1,@H_404_2@ wrapper: {@H_404_2@@H_404_2@ },@H_404_2@ slide: {@H_404_2@ justifyContent: 'center',@H_404_2@ backgroundColor: 'transparent'@H_404_2@ },@H_404_2@ image: {@H_404_2@ width:width,@H_404_2@ height: 130,@H_404_2@});@H_404_2@

安卓和苹果模拟器里最终运行的视频效果如下:

https://pan.baidu.com/s/1eZ9GZts9ILeT7NEpcuIOqw

猜你在找的React相关文章