-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
217 lines (200 loc) · 4.95 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
Platform,
BackAndroid,
ToastAndroid,
} from 'react-native';
//引入tabbar支持包
import TabNavigator from 'react-native-tab-navigator';
//首页
import Home from './Home';
import Hot from './hot';
import Fun from './fun';
import City from './city';
import Community from './community';
const TabNavigatorItem =TabNavigator.Item;
const TAB_NORMAL_1=require('image!tab_1');
const TAB_NORMAL_2=require('image!tab_2');
const TAB_NORMAL_3=require('image!tab_3');
const TAB_NORMAL_4=require('image!tab_4');
const TAB_NORMAL_5=require('image!tab_5');
const TAB_PRESS_1=require('image!tab_press_1');
const TAB_PRESS_2=require('image!tab_press_2');
const TAB_PRESS_3=require('image!tab_press_3');
const TAB_PRESS_4=require('image!tab_press_4');
const TAB_PRESS_5=require('image!tab_press_5');
export default class app extends Component {
constructor(){
super();
this.state={
selectedTab:'Home',
}
}
componentWillMount(){
if(Platform.OS==='android'){
BackAndroid.addEventListener('hardwareBackPress',this.onBackAndroid.bind(this));
}
}
componentWillUnmount(){
if(Platform.OS==='android'){
BackAndroid.removeEventListener('hardwareBackPress',this.onBackAndroid.bind(this));
}
}
/**
返回true:执行back,返回false,执行eixt
**/
onBackAndroid(){
const nav = this.props.navigator;
const routers = nav.getCurrentRoutes();
if(routers.length>1){
nav.pop();
return true;
}
if(this.lastBackPressed&&(this.lastBackPressed+2000>=Date.now())){
//两秒内back处理
return false;
}else{
this.lastBackPressed = Date.now();
ToastAndroid.show('在按一次退出程序',ToastAndroid.SHORT);
return true;
}
}
/**
tab点击方法
**/
onPress(tabName){
if(tabName){
this.setState(
{
selectedTab:tabName,
}
);
}
}
/**
渲染每项
**/
renderTabView(title,tabName,tabContent,isBadge){
var tabNomal;
var tabPress;
var component;
switch (tabName) {
case 'Home':
tabNomal=TAB_NORMAL_1;
tabPress=TAB_PRESS_1;
component=<Home navigator={this.props.navigator}/>;
break;
case 'hot':
tabNomal=TAB_NORMAL_2;
tabPress=TAB_PRESS_2;
component=<Hot/>;
break;
case 'city':
tabNomal=TAB_NORMAL_3;
tabPress=TAB_PRESS_3;
component=<City/>;
break;
case 'fun':
tabNomal=TAB_NORMAL_4;
tabPress=TAB_PRESS_4;
component=<Fun/>;
break;
case 'Community':
tabNomal=TAB_NORMAL_5;
tabPress=TAB_PRESS_5;
component=<Community/>;
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}
>
{component}
</TabNavigatorItem>
);
}
/**
自定义tabbar
**/
tabBarView(){
return (
<View style={{flex:1}}>
<TabNavigator
tabBarStyle={styles.tab}
>
{this.renderTabView('订阅','Home','头条板块',false)}
{this.renderTabView('热点','hot','视频板块',false)}
{this.renderTabView('上海','city','关注板块',false)}
{this.renderTabView('玩乐','fun','我的板块',false)}
{this.renderTabView('社区','Community','我的板块',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,
}
});