React Native 实现基于react-native-tab-navigator库Tab切换封装

来源:互联网 发布:淘宝卖无印良品的好店 编辑:程序博客网 时间:2024/06/12 23:05

react-native-tab-navigator是一款Tab切换的库,细心的读者可能注意到了对于TabNavigator.Item选项卡部分,代码功能上基本上是重复的,对此,我们能不能对这种有相同功能的代码进行二次封装呢?

这里写图片描述

代码示例

新建项目,并安装react-native-tab-navigator库支持

npm install react-native-tab-navigator –save

主页面封装

首先我们可以将功能的部分抽出来。

<TabNavigatorItem        selected={this.state.selectedTab===tabName}        title={title}        titleStyle={styles.tabText}        selectedTitleStyle={styles.selectedTabText}        renderIcon={()=><Image style={styles.icon} source={tabNomal}/>}        renderSelectedIcon={()=><Image style={styles.icon} source={tabPress}/>}        onPress={()=>this.onPress(tabName)}        renderBadge={()=>isBadge?<View style={styles.badgeView}><Text style={styles.badgeText}>15</Text></View>:null}        >       <View style={styles.page}>        <Text>{tabContent}</Text>       </View>       </TabNavigatorItem>

接下来我们需要构造一个函数,用来接收不同情况下Tab子选项卡。

switch (tabName) {        case 'Home':          tabNomal=TAB_HOME_NORMAL;          tabPress=TAB_HOME_PRESS;          break;       case 'Mine':        tabNomal=TAB_MINE_NORMAL;        tabPress=TAB_MINE_PRESS;        break;        default:      }

所以构造的完整的代码如:

//名称,图标,子视图文本,选中状态renderTabView(title,tabName,tabContent,isBadge){      var tabNomal;      var tabPress;      switch (tabName) {        case 'Home':          tabNomal=TAB_HOME_NORMAL;          tabPress=TAB_HOME_PRESS;          break;       case 'Mine':        tabNomal=TAB_MINE_NORMAL;        tabPress=TAB_MINE_PRESS;        break;        default:      }      return(       <TabNavigatorItem        selected={this.state.selectedTab===tabName}        title={title}        titleStyle={styles.tabText}        selectedTitleStyle={styles.selectedTabText}        renderIcon={()=><Image style={styles.icon} source={tabNomal}/>}        renderSelectedIcon={()=><Image style={styles.icon} source={tabPress}/>}        onPress={()=>this.onPress(tabName)}        renderBadge={()=>isBadge?<View style={styles.badgeView}><Text style={styles.badgeText}>15</Text></View>:null}        >       <View style={styles.page}>        <Text>{tabContent}</Text>       </View>       </TabNavigatorItem>     );   }

其实到此,我们已经实现了封装,在使用的时候按如下方式直接使用即可:

{this.renderTabView('首页','Home','首页模块',true)}

但是,我们对上面的内容还可以进一步的封装:

tabBarView(){          return (            <TabNavigator             tabBarStyle={styles.tab}>            {renderTabView('首页','Home','首页模块',true)}            {renderTabView('我的','Mine','我的模块',false)}            </TabNavigator>          );        }

然后,我们在render()只需要简单的调用即可:

render() {    var tabView=tabBarView();    return (      <View style={styles.container}>             {tabView}            </View>    );  }

那么完整的代码如下:

/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import TabNavigator from 'react-native-tab-navigator'; import HomeScreen from './src/widght/HomeScreen'; import MineScreen from './src/widght/MineScreen'; import {   AppRegistry,   StyleSheet,   Text,   Image,   View } from 'react-native'; const TabNavigatorItem =TabNavigator.Item; //默认选项 const TAB_HOME_NORMAL=require('./image/tabbar_homepage.png'); const TAB_MINE_NORMAL=require('./image/tabbar_mine.png'); //选中 const TAB_HOME_PRESS=require('./image/tabbar_homepage_selected.png'); const TAB_MINE_PRESS=require('./image/tabbar_mine_selected.png');export default class HelloWord extends Component {   //默认选中   constructor(){     super();     this.state={       selectedTab:'Home',     }   }   //点击方法   onPress(tabName){    if(tabName){      this.setState({          selectedTab:tabName,        }      );    }  } //渲染选项卡  renderTabView(title,tabName,defaultTab,isBadge){       var tabNomal;       var tabPress;       var tabPage;       switch (tabName) {         case 'Home':           tabNomal=TAB_HOME_NORMAL;           tabPress=TAB_HOME_PRESS;           tabPage=<HomeScreen/>;           break;        case 'Mine':         tabNomal=TAB_MINE_NORMAL;         tabPress=TAB_MINE_PRESS;         tabPage=<MineScreen/>;         break;         default:       }       return(        <TabNavigatorItem         selected={this.state.selectedTab===tabName}         title={title}         titleStyle={styles.tabText}         selectedTitleStyle={styles.selectedTabText}         renderIcon={()=><Image style={styles.icon} source={tabNomal}/>}         renderSelectedIcon={()=><Image style={styles.icon} source={tabPress}/>}         onPress={()=>this.onPress(tabName)}         renderBadge={()=>isBadge?<View style={styles.badgeView}><Text style={styles.badgeText}>15</Text></View>:null}         >        <View style={styles.page}>         {tabPage}        </View>        </TabNavigatorItem>      );    }       //自定义TabView     tabBarView(){           return (             <TabNavigator              tabBarStyle={styles.tab}>             {this.renderTabView('首页','Home',HomeScreen,true)}             {this.renderTabView('我的','Mine',HomeScreen,false)}             </TabNavigator>           );         }   //渲染界面   render() {     var tabView=this.tabBarView();     return (       <View style={styles.container}>              {tabView}             </View>     );   } } const styles = StyleSheet.create({   container: {     flex: 1   },   tabText: {         fontSize: 10,         color: 'black'     },     selectedTabText: {         fontSize: 10,         color: 'green'     },    tabIcon:{     width:25,     height:25,   },   badgeView:{     width:22,     height:14 ,     backgroundColor:'#f85959',     borderWidth:1,     marginLeft:10,     marginTop:3,     borderColor:'#FFF',     alignItems:'center',     justifyContent:'center',     borderRadius:8,   },   badgeText:{     color:'#ffffff',     fontSize:8,   },     icon: {         width: 22,         height: 22     },     page: {         flex: 1,         justifyContent: 'center',         alignItems: 'center',         backgroundColor: '#FFFFFF'     }, });AppRegistry.registerComponent('HelloWord', () => HelloWord);

标题栏封装

接下来我们对标题栏进行封装,注意,标题是变化的,这时候我们想到给Text的props设置动态属性。在使用的时候直接简单的调用下即可。

<Header text='首页'> </Header>

完整代码:

/** * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { View, Text,StyleSheet} from 'react-native';var Dimensions = require('Dimensions');var ScreenWidth = Dimensions.get('window').width;class Header extends Component {    render() {        return (          <View style={styles.container}>           <Text style={styles.text }>{this.props.text || "标题头"}</Text>          <Text style={styles.diviceLine}/>         </View>      );    }}const styles = StyleSheet.create({    container: {      width:ScreenWidth,      marginTop:20,      height:50,      alignItems:'center', /*水平居中*/      justifyContent:'center',/*垂直居中*/      backgroundColor: '#FFFFFF',      flexDirection:'column'    },    text: {        fontSize: 18,        color:'#000000',        textAlign:'center'    },    diviceLine: {        backgroundColor: '#e9e9e9',        height: 1,     }});export default Header;

源码地址

2 2
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝申请退款后卖家不确认怎么办 淘宝直通车b类扣12分怎么办 淘宝店铺被买家投诉怎么办 淘宝店铺遭买家投诉怎么办 淘宝店铺被买家恶意投诉怎么办 苹果手机无法识别指纹怎么办 淘宝店铺虚假交易违规怎么办 斗鱼直播太卡怎么办 神笔添加视频尺码不符合怎么办 闲鱼卖家单号填错了怎么办 户户通没有信号强度怎么办 全民k歌qq登不上怎么办 手机直播没电了怎么办 淘宝退货卖家拒收怎么办 充的会员卡店家关门了怎么办 淘宝手机号码被注册了怎么办 淘宝不支持七天无理由退货怎么办 微信视频横屏怎么办 房子里潮气太重怎么办 淘宝不小心注销了怎么办 淘宝号不小心注销了怎么办 xp网络驱动没了怎么办 淘宝卖家客服态度差怎么办 怀孕吃辣椒喉咙好痛怎么办 淘宝店铺建议不要提交认证怎么办 淘宝买东西商家不退款怎么办 淘宝买东西商家不发货怎么办 在微信上买东西被骗了怎么办 新浪微博自动关注人怎么办 搜淘宝找不到关键词和店铺怎么办 小超市开在一起竞争太大怎么办 淘宝店铺被投诉盗图怎么办 充电宝ic坏了怎么办 淘宝店宝贝权重下降怎么办 淘宝卖家评分低怎么办 淘宝买东西客服不理人怎么办 支付宝本次交易嫌疑违规怎么办 支付宝一年的交易总额怎么办 交易关闭钱扣了怎么办 交易猫账号绑定支付宝打不开怎么办 拼多多涉假处罚怎么办