React-Native入门指南

    二、分解内容

    1. 整个页面我们可以分为几个部分,大致如下:
    • 头部
    • 图片轮播
    • 9宫格
    • 底部活动

    四、图片轮播

    1. 这里图片轮播使用的是第三方组件react-native-swiper,当然React-Native是支持transform可以直接实现一套。我们启动npm命令行,在项目的根目录使用如下命令安装模块。
    2. $ npm install react-native-swiper --save
    3. $ npm install react-timer-mixin --save
    1. 安装完成后,我们需要完成轮播功能。因为可以到github看看swiper暴露的接口和参数。github地址是:https://github.com/leecade/react-native-swiper
    2. 1)引入swiper,前面也提到了require.
    3. var Swiper = require('react-native-swiper');
    4. 2)使用swiper,将轮播图封装成单独的组件
    5. var sliderImgs = [
    6. 'http://images3.c-ctrip.com/SBU/apph5/201505/16/app_home_ad16_640_128.png',
    7. 'http://images3.c-ctrip.com/rk/apph5/C1/201505/app_home_ad49_640_128.png',
    8. 'http://images3.c-ctrip.com/rk/apph5/D1/201506/app_home_ad05_640_128.jpg'
    9. ];
    10. var Slider = React.createClass({
    11. render: function(){
    12. return (
    13. <Swiper style={styles.wrapper} showsButtons={false} autoplay={true} height={150} showsPagination={false}>
    14. <Image style={[styles.slide,]} source={{uri: sliderImgs[0]}}></Image>
    15. <Image style={[styles.slide,]} source={{uri: sliderImgs[1]}}></Image>
    16. <Image style={[styles.slide,]} source={{uri: sliderImgs[2]}}></Image>
    17. </Swiper>
    18. );
    19. }
    20. });
    21. (3)这样我们可以直接在render的时候直接用:<Slider/>

    六、样式类

    1. 说完了布局的原理,这里需要贴上样式仅供参考:
    2. var styles = StyleSheet.create({
    3. //container
    4. container:{
    5. flex:1,
    6. },
    7. //slider
    8. wrapper: {
    9. height:80,
    10. },
    11. slide: {
    12. resizeMode: Image.resizeMode.contain,
    13. },
    14. //sbu
    15. sbu_view:{
    16. height:84,
    17. marginLeft: 5,
    18. marginRight:5,
    19. borderWidth:1,
    20. borderRadius:5,
    21. marginBottom:10,
    22. flexDirection:'row',
    23. },
    24. sbu_red:{
    25. backgroundColor: '#FA6778',
    26. borderColor:'#FA6778',
    27. marginTop:-70,
    28. },
    29. sbu_blue:{
    30. backgroundColor: '#3D98FF',
    31. borderColor:'#3D98FF',
    32. },
    33. sbu_green:{
    34. backgroundColor: '#5EBE00',
    35. borderColor:'#5EBE00',
    36. },
    37. sbu_yellow:{
    38. backgroundColor: '#FC9720',
    39. borderColor:'#FC9720',
    40. },
    41. sbu_flex:{
    42. flex:1,
    43. },
    44. sbu_borderRight:{
    45. borderColor:'#fff',
    46. },
    47. sbu_icon_img:{
    48. width:40,
    49. resizeMode:Image.resizeMode.contain,
    50. },
    51. sub_con_flex:{
    52. flex:1,
    53. justifyContent: 'center',
    54. alignItems: 'center',
    55. },
    56. sub_text:{
    57. justifyContent:'center',
    58. },
    59. font16:{
    60. fontSize:17,
    61. color:'#FFF',
    62. fontWeight:'900',
    63. },
    64. sbu_borderBottom:{
    65. borderBottomWidth:0.5,
    66. borderBottomColor:'#fff',
    67. },
    68. img_view:{
    69. height:62,
    70. marginLeft:5,
    71. marginRight:5,
    72. flexDirection: 'row',
    73. marginBottom:20,
    74. backgroundColor:'#fff',
    75. },
    76. img_flex:{
    77. flex:1,
    78. borderWidth:1,
    79. borderColor:'#ccc',
    80. },
    81. img_wh: {
    82. height:59,
    83. borderRightWidth:0,
    84. resizeMode:Image.resizeMode.contain,
    85. }
    86. });
    1. 实例代码中会涉及ScrollView组件,主要是为了适应小屏的机器,可以滚动视图。