Skip to content

Commit 9eb8fb7

Browse files
author
weimeng
committed
1. 增加Tabbar 2. 增加轮播图
1 parent 0379680 commit 9eb8fb7

20 files changed

Lines changed: 639 additions & 73 deletions

app/basic/ListView.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ export class ListView extends Component {
260260
onScroll={this._scroll.bind(this)}
261261
scrollEventThrottle={15}
262262
refreshControl={this.props.refreshControl}
263-
263+
style={{height : this.props.height}}
264+
265+
264266
>
265267
<View style={{height : H1}}></View>
266268
{

app/domain/component/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424

2525
export * from "./ZButton"
2626
export * from "./navbar/ZNavBar"
27+
export * from "./tabbar/Tabbar"
2728

2829

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/***********************************************
2+
*
3+
* MIT License
4+
*
5+
* Copyright (c) 2016 珠峰课堂,Ramroll
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
27+
import React, {Component} from 'react'
28+
29+
import {
30+
31+
View,
32+
Text,
33+
Image,
34+
TouchableOpacity
35+
} from 'react-native'
36+
37+
import {flexCenter} from 'basic'
38+
39+
export class Tabbar extends Component {
40+
41+
constructor(props){
42+
super()
43+
this.state = {
44+
active : props.initialActive
45+
}
46+
}
47+
48+
_press(name){
49+
50+
return () => {
51+
this.setState({
52+
active : name
53+
})
54+
}
55+
56+
57+
}
58+
59+
render() {
60+
const {children } = this.props
61+
const {active} = this.state
62+
63+
return (
64+
<View style={{flex : 1}}>
65+
<View style={{flex : 1}}>
66+
{children.map((child, i) => {
67+
return React.cloneElement(child, {active, key : i})
68+
})}
69+
</View>
70+
<View style={{height : 60, flexDirection : "row", borderTopWidth : 1, borderColor : "#f2f3f4" }}>
71+
{children.map((child, i) => {
72+
73+
const {title, name, icon, activeIcon} = child.props
74+
75+
console.log(name, active)
76+
return <TouchableOpacity onPress={this._press(name).bind(this)} key={i} style={{flex : 1, height : 60, ...flexCenter}}>
77+
<Image source={name == active ? activeIcon : icon} resizeMode="contain" style={{width : 28, height : 28, marginBottom : 5}} />
78+
<Text>{title}</Text>
79+
</TouchableOpacity>
80+
81+
82+
83+
})}
84+
85+
</View>
86+
</View>
87+
)
88+
}
89+
}
90+
91+
92+
class TabbarItem extends Component{
93+
94+
render(){
95+
96+
const {children, name, active} = this.props
97+
const style = {overflow : 'hidden'}
98+
if(active != name) {
99+
style.height = 0
100+
}
101+
return <View style={style}>
102+
{children}
103+
</View>
104+
105+
}
106+
}
107+
108+
Tabbar.Item = TabbarItem
109+
110+
111+

app/domain/page/ExampleListView.js

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/***********************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2016 珠峰课堂,Ramroll
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*
23+
*/
24+
25+
26+
import React, {Component} from 'react'
27+
28+
import {
29+
View,
30+
Text,
31+
Image,
32+
Dimensions,
33+
ScrollView,
34+
ActivityIndicator,
35+
StyleSheet,
36+
RefreshControl
37+
} from 'react-native'
38+
39+
import {format_currency, flexCenter} from "basic"
40+
41+
42+
import {COLOR_TITLE, COLOR_TEXT_LIGHT, COLOR_PRICE} from "domain/def"
43+
44+
45+
import {ListView} from 'basic'
46+
47+
48+
49+
const course_gen = () => {
50+
return {
51+
image : "http://a.hiphotos.baidu.com/image/h%3D200/sign=af9259bf03082838770ddb148898a964/6159252dd42a2834bc76c4ab5fb5c9ea14cebfba.jpg",
52+
title : "顶级大神教你写node.js",
53+
author : "张仁阳",
54+
description : "国内顶尖大神教你写node.js.从零开始,循序渐进.......",
55+
price : Math.random() * 10000 + 5000
56+
}
57+
}
58+
59+
60+
export class Home extends Component {
61+
62+
63+
constructor(){
64+
super()
65+
66+
67+
68+
const courses = []
69+
70+
for(let i = 0; i < 20; i++) {
71+
courses.push(course_gen())
72+
}
73+
74+
75+
this.state = {
76+
courses : courses,
77+
loading : false
78+
}
79+
}
80+
81+
_renderItem(course, i) {
82+
return (
83+
<CourseCard {...course} />
84+
)
85+
}
86+
87+
88+
_onScrollToBottom(y){
89+
90+
this.y = y
91+
console.log(y)
92+
}
93+
94+
_renderBottomIndicator(){
95+
96+
const indicator = this.y < 100 ?
97+
<ScrollIndicator image={require("./arrow_down.png")}>下拉加载更多</ScrollIndicator>
98+
: <ScrollIndicator image={require("./arrow_up.png")}>释放加载更多</ScrollIndicator>
99+
100+
return (
101+
<View style={{height : 42, ...flexCenter}}>
102+
{this.state.loading ?
103+
<ActivityIndicator />
104+
:
105+
indicator
106+
}
107+
</View>
108+
)
109+
}
110+
111+
_release(){
112+
if(this.y > 100 && !this.state.loading) {
113+
this.setState({ loading : true }, (() => {
114+
}).bind(this))
115+
116+
setTimeout((() => {
117+
const courses = []
118+
for(let i = 0; i < 20; i++) {
119+
courses.push(course_gen())
120+
}
121+
this.refs.listView.append(courses)
122+
this.setState({
123+
loading : false
124+
})
125+
}).bind(this), 2000)
126+
}
127+
}
128+
129+
130+
131+
_refresh(){
132+
133+
if(!this.state.loading) {
134+
135+
this.setState({loading : true}, (() => {
136+
setTimeout((() => {
137+
const courses = []
138+
for(let i = 0; i < 20; i++) {
139+
courses.push(course_gen())
140+
}
141+
this.refs.listView.reset(courses)
142+
this.setState({
143+
loading : false
144+
})
145+
}).bind(this), 2000)
146+
}).bind(this))
147+
}
148+
}
149+
150+
render(){
151+
152+
const loading = this.state
153+
return (
154+
<View>
155+
<ListView
156+
ref="listView"
157+
initialData={this.state.courses}
158+
renderItem={this._renderItem}
159+
onScrollToBottom={this._onScrollToBottom.bind(this)}
160+
renderBottomIndicator={this._renderBottomIndicator.bind(this)}
161+
onResponderRelease={this._release.bind(this)}
162+
refreshControl={
163+
<RefreshControl refreshing={false} onRefresh={this._refresh.bind(this)} />
164+
}
165+
/>
166+
</View>
167+
)
168+
}
169+
170+
}
171+
172+
class CourseCard extends Component{
173+
174+
render() {
175+
const W = Dimensions.get("window").width
176+
177+
const {image, title, author, description, price} = this.props
178+
179+
return <View style={courseStyle.cardContainer}>
180+
<Image
181+
source={{uri : image}}
182+
style={{width : W - 20, height : (W - 20) * 0.3}}
183+
/>
184+
<Title>{title}</Title>
185+
<Author label="讲师">{author}</Author>
186+
<Description>{description}</Description>
187+
<Price>{price}</Price>
188+
</View>
189+
}
190+
191+
}
192+
193+
const courseStyle = StyleSheet.create({
194+
cardContainer: {
195+
marginBottom: 20,
196+
paddingBottom: 10,
197+
marginLeft: 10,
198+
marginRight: 10, borderRadius: 5, overflow: "hidden", borderWidth: 1, borderColor: "#c7c8c9",
199+
}
200+
}
201+
)
202+
203+
const Paragraph = {
204+
paddingLeft : 20,
205+
paddingRight : 20,
206+
marginTop : 10
207+
208+
}
209+
210+
211+
212+
const Title = ({children}) => {
213+
return <Text style={{...Paragraph, color : COLOR_TITLE, fontSize : 18, fontWeight : 'bold'}}>{children}</Text>
214+
215+
}
216+
/**
217+
* 等同于
218+
* class Title extends Component {
219+
* render() {
220+
* const {children} = this.props
221+
* ....
222+
* }
223+
* }
224+
*
225+
*/
226+
227+
const ScrollIndicator = ({children, image}) => {
228+
return <View style={{flexDirection : 'row', ...flexCenter}}>
229+
<Text>{children}</Text>
230+
<Image source={image} style={{width : 18, height : 18}} />
231+
</View>
232+
233+
234+
}
235+
236+
237+
const Author = ({label, children}) => {
238+
return <Text style={{...Paragraph, color : COLOR_TEXT_LIGHT}}>{label}:{children}</Text>
239+
}
240+
241+
242+
const Description = ({children}) => {
243+
return <Text style={{...Paragraph, color : COLOR_TEXT_LIGHT}}>{children}</Text>
244+
}
245+
246+
// 1000 => 1,000.00
247+
// 1000.000 => 1,000.00
248+
249+
250+
251+
const Price = ({children}) => {
252+
return <Text style={{...Paragraph, color : COLOR_PRICE,fontSize : 18, fontWeight : 'bold'}}>{format_currency(children)}</Text>
253+
}

0 commit comments

Comments
 (0)