CategorySelector.js
4.26 KB
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
'use strict';
import React, {Component} from 'react';
import {Dimensions, ListView, StyleSheet, Text, TouchableOpacity, View,} from 'react-native';
import Immutable from 'immutable';
export default class CategorySelector extends Component {
constructor(props) {
super(props);
this.dataSource = new ListView.DataSource({
rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
});
this.renderRow = this.renderRow.bind(this);
this.scrollToCenter = this.scrollToCenter.bind(this);
this.listView = null;
this.scrollContentWidth = 0;
this.rowPosition = {};
}
scrollToCenter(rowID) {
if (!this.listView) {
return;
}
let rowLayout = this.rowPosition[rowID];
if (!rowLayout) {
return;
}
let rowX = rowLayout.x;
let rowWidth = rowLayout.width;
if (this.scrollContentWidth < width) {
return;
}
// 位于列表头,且x坐标 < 屏幕x中心距离
if (rowX + rowWidth / 2 < width / 2) {
this.listView && this.listView.scrollTo({x: 0, y: 0, animated: true});
return;
}
// 位于列表尾,且(容器宽度-x坐标)< 屏幕x中心距离
if ((this.scrollContentWidth - rowX - rowWidth / 2) < width / 2) {
this.listView && this.listView.scrollTo({x: this.scrollContentWidth - width, y: 0, animated: true});
return;
}
let scrollX = rowX + rowWidth / 2 - width / 2 + 5;
this.listView && this.listView.scrollTo({x: scrollX, y: 0, animated: true});
}
renderRow(rowData, sectionID, rowID, highlightRow) {
let marginLeft = (rowID == 0) ? 0 : 22;
let color = rowID == this.props.selectedCategoryIndex ? '#444444' : '#b0b0b0';
let fontWeight = rowID == this.props.selectedCategoryIndex ? 'bold' : 'normal';
return (
<TouchableOpacity
key={'row' + rowID}
activeOpacity={1}
style={styles.rowContainer}
onPress={() => {
this.scrollToCenter(rowID);
this.props.onPressCategory && this.props.onPressCategory(rowData, rowID);
}}
onLayout={(event) => {
this.rowPosition[rowID] = event.nativeEvent.layout;
}}
>
<View style={[styles.categoryContainer, {marginLeft}]}>
<Text style={[styles.categoryName, {color, fontWeight}]}>{rowData.get('sort_name')}</Text>
</View>
</TouchableOpacity>
);
}
render() {
let data = this.props.data;
return (
<View style={styles.container}>
<ListView
ref={(c) => {
this.listView = c;
}}
enableEmptySections={true}
contentContainerStyle={styles.contentContainer}
dataSource={this.dataSource.cloneWithRows(data.toArray())}
renderRow={this.renderRow}
horizontal={true}
showsHorizontalScrollIndicator={false}
onContentSizeChange={(contentWidth, contentHeight) => {
this.scrollContentWidth = contentWidth;
}}
/>
{data.size > 0 ? <View style={styles.line}/> : null}
</View>
);
}
};
let {width} = Dimensions.get('window');
const DEVICE_WIDTH_RATIO = width / 320;
let imageWidth = Math.floor(width / 2);
let imageHeight = Math.floor(imageWidth * 180 / 375);
let selectorHeight = Math.ceil(45 * DEVICE_WIDTH_RATIO);
selectorHeight = 45;
let styles = StyleSheet.create({
container: {
height: 44,
backgroundColor: 'white',
flexDirection: 'row',
},
contentContainer: {
marginLeft: 15,
},
rowContainer: {
justifyContent: 'center',
},
categoryContainer: {
marginRight: 22
},
categoryName: {
fontSize: 14,
color: '#b0b0b0',
},
line: {
position: 'absolute',
width,
height: 1,
left: 0,
bottom: 0,
backgroundColor: '#e5e5e5',
},
});