forked from jjhappyforever/TouTiaoTabBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
170 lines (156 loc) · 3.74 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
Platform,
StatusBar,
} from 'react-native';
//引入tabbar支持包
import TabNavigator from 'react-native-tab-navigator';
//首页
import Home from './view/Home';
const TabNavigatorItem =TabNavigator.Item;
const TAB_NORMAL_1=require('./images/tabbar_1.png');
const TAB_NORMAL_2=require('./images/tabbar_2.png');
const TAB_NORMAL_3=require('./images/tabbar_3.png');
const TAB_NORMAL_4=require('./images/tabbar_4.png');
const TAB_PRESS_1=require('./images/tabbar_1_press.png');
const TAB_PRESS_2=require('./images/tabbar_2_press.png');
const TAB_PRESS_3=require('./images/tabbar_3_press.png');
const TAB_PRESS_4=require('./images/tabbar_4_press.png');
export default class toutiao extends Component {
constructor(){
super();
this.state={
selectedTab:'Home',
}
}
/**
tab点击方法
**/
onPress(tabName){
if(tabName){
this.setState(
{
selectedTab:tabName,
}
);
}
}
/**
渲染每项
**/
renderTabView(title,tabName,tabContent,isBadge){
var tabNomal;
var tabPress;
switch (tabName) {
case 'Home':
tabNomal=TAB_NORMAL_1;
tabPress=TAB_PRESS_1;
break;
case 'Video':
tabNomal=TAB_NORMAL_2;
tabPress=TAB_PRESS_2;
break;
case 'Follow':
tabNomal=TAB_NORMAL_3;
tabPress=TAB_PRESS_3;
break;
case 'Mine':
tabNomal=TAB_NORMAL_4;
tabPress=TAB_PRESS_4;
break;
default:
}
return(
<TabNavigatorItem
title={title}
renderIcon={()=><Image style={styles.tabIcon} source={tabNomal}/>}
renderSelectedIcon={()=><Image style={styles.tabIcon} source={tabPress}/>}
selected={this.state.selectedTab===tabName}
selectedTitleStyle={{color:'#f85959'}}
onPress={()=>this.onPress(tabName)}
renderBadge={()=>isBadge?<View style={styles.badgeView}><Text style={styles.badgeText}>15</Text></View>:null}
>
{
tabName=='Home'?<Home/>:
<View style={{flex:1,justifyContent:'center',alignItems:'center'}}><Text>{tabContent}</Text></View>
}
</TabNavigatorItem>
);
}
/**
自定义tabbar
**/
tabBarView(){
return (
<View style={{flex:1}}>
<TabNavigator
tabBarStyle={styles.tab}
>
{this.renderTabView('头条','Home','头条板块',true)}
{this.renderTabView('视频','Video','视频板块',false)}
{this.renderTabView('关注','Follow','关注板块',false)}
{this.renderTabView('我的','Mine','我的板块',false)}
</TabNavigator>
</View>
);
}
render() {
var tabBarView=this.tabBarView();
return (
<View style={styles.container}>
{tabBarView}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
tab:{
height: 52,
alignItems:'center',
backgroundColor:'#f4f5f6',
},
tabIcon:{
width:25,
height:25,
},
badgeView:{
width:22,
height:14 ,
backgroundColor:'#f85959',
borderWidth:1,
marginLeft:10,
marginTop:5,
borderColor:'#FFF',
alignItems:'center',
justifyContent:'center',
borderRadius:8,
},
badgeText:{
color:'#fff',
fontSize:8,
}
});