react-native城市列表组件
时间:2022-04-27
本文章向大家介绍react-native城市列表组件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
城市列表选择是很多app共有的功能,比如典型的美图app。那么对于React Native怎么实现呢?
要实现上面的效果,首先需要对界面的组成简单分析,界面的数据主要由当前城市,历史访问城市和热门城市组成,所以我们在提供Json数据的时候就需要将数据分为至少3部分。
const ALL_CITY_LIST = DATA_JSON.allCityList;
const HOT_CITY_LIST = DATA_JSON.hotCityList;
const LAST_VISIT_CITY_LIST = DATA_JSON.lastVisitCityList;
而要实现字母索引功能,我们需要自定义一个控件,实现和数据的绑定关系,自定义组件代码如下: CityIndexListView.js
'use strict';
import React, {Component} from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
ListView,
Dimensions,
} from 'react-native';
import Toast, {DURATION} from './ToastUtil'
const SECTIONHEIGHT = 30;
const ROWHEIGHT = 40;
const ROWHEIGHT_BOX = 40;
var totalheight = []; //每个字母对应的城市和字母的总高度
const {width, height} = Dimensions.get('window');
var that;
const key_now = '当前';
const key_last_visit = '最近';
const key_hot = '热门';
export default class CityIndexListView extends Component {
constructor(props) {
super(props);
var getSectionData = (dataBlob, sectionID) => {
return sectionID;
};
var getRowData = (dataBlob, sectionID, rowID) => {
return dataBlob[sectionID][rowID];
};
let ALL_CITY_LIST = this.props.allCityList;
let CURRENT_CITY_LIST = this.props.nowCityList;
let LAST_VISIT_CITY_LIST = this.props.lastVisitCityList;
let HOT_CITY_LIST = this.props.hotCityList;
let letterList = this._getSortLetters(ALL_CITY_LIST);
let dataBlob = {};
dataBlob[key_now] = CURRENT_CITY_LIST;
dataBlob[key_last_visit] = LAST_VISIT_CITY_LIST;
dataBlob[key_hot] = HOT_CITY_LIST;
ALL_CITY_LIST.map(cityJson => {
let key = cityJson.sortLetters.toUpperCase();
if (dataBlob[key]) {
let subList = dataBlob[key];
subList.push(cityJson);
} else {
let subList = [];
subList.push(cityJson);
dataBlob[key] = subList;
}
});
let sectionIDs = Object.keys(dataBlob);
let rowIDs = sectionIDs.map(sectionID => {
let thisRow = [];
let count = dataBlob[sectionID].length;
for (let ii = 0; ii < count; ii++) {
thisRow.push(ii);
}
let eachheight = SECTIONHEIGHT + ROWHEIGHT * thisRow.length;
if (sectionID === key_hot || sectionID === key_now || sectionID === key_last_visit) {
let rowNum = (thisRow.length % 3 === 0)
? (thisRow.length / 3)
: parseInt(thisRow.length / 3) + 1;
console.log('sectionIDs===>' + sectionIDs + ", rowNum=====>" + rowNum);
eachheight = SECTIONHEIGHT + ROWHEIGHT_BOX * rowNum;
}
totalheight.push(eachheight);
return thisRow;
});
let ds = new ListView.DataSource({
getRowData: getRowData,
getSectionHeaderData: getSectionData,
rowHasChanged: (row1, row2) => row1 !== row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
});
this.state = {
dataSource: ds.cloneWithRowsAndSections(dataBlob, sectionIDs, rowIDs),
letters: sectionIDs
};
that = this;
}
_getSortLetters(dataList) {
let list = [];
for (let j = 0; j < dataList.length; j++) {
let sortLetters = dataList[j].sortLetters.toUpperCase();
let exist = false;
for (let xx = 0; xx < list.length; xx++) {
if (list[xx] === sortLetters) {
exist = true;
}
if (exist) {
break;
}
}
if (!exist) {
list.push(sortLetters);
}
}
return list;
}
_cityNameClick(cityJson) {
// alert('选择了城市====》' + cityJson.id + '#####' + cityJson.name);
this.props.onSelectCity(cityJson);
}
_scrollTo(index, letter) {
this.refs.toast.close();
let position = 0;
for (let i = 0; i < index; i++) {
position += totalheight[i]
}
this._listView.scrollTo({y: position});
this.refs.toast.show(letter, DURATION.LENGTH_SHORT);
}
_renderRightLetters(letter, index) {
return (
<TouchableOpacity key={'letter_idx_' + index} activeOpacity={0.6} onPress={() => {
this._scrollTo(index, letter)
}}>
<View style={styles.letter}>
<Text style={styles.letterText}>{letter}</Text>
</View>
</TouchableOpacity>
);
}
_renderListBox(cityJson, rowId) {
return (
<TouchableOpacity key={'list_item_' + cityJson.id} style={styles.rowViewBox} onPress={() => {
that._cityNameClick(cityJson)
}}>
<View style={styles.rowdataBox}>
<Text style={styles.rowDataTextBox}>{cityJson.name}</Text>
</View>
</TouchableOpacity>
);
}
_renderListRow(cityJson, rowId) {
console.log('rowId===>' + rowId + ", cityJson====>" + JSON.stringify(cityJson));
if (rowId === key_now || rowId === key_hot || rowId === key_last_visit) {
return that._renderListBox(cityJson, rowId);
}
return (
<TouchableOpacity key={'list_item_' + cityJson.id} style={styles.rowView} onPress={() => {
that._cityNameClick(cityJson)
}}>
<View style={styles.rowdata}>
<Text style={styles.rowdatatext}>{cityJson.name}</Text>
</View>
</TouchableOpacity>
)
}
_renderListSectionHeader(sectionData, sectionID) {
return (
<View style={styles.sectionView}>
<Text style={styles.sectionText}>
{sectionData}
</Text>
</View>
);
}
render() {
return (
<View style={styles.container}>
<View style={styles.listContainner}>
<ListView ref={listView => this._listView = listView}
contentContainerStyle={styles.contentContainer} dataSource={this.state.dataSource}
renderRow={this._renderListRow} renderSectionHeader={this._renderListSectionHeader}
enableEmptySections={true} initialListSize={500}/>
<View style={styles.letters}>
{this.state.letters.map((letter, index) => this._renderRightLetters(letter, index))}
</View>
</View>
<Toast ref="toast" position='top' positionValue={200} fadeInDuration={750} fadeOutDuration={1000}
opacity={0.8}/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
// paddingTop: 50,
flex: 1,
flexDirection: 'column',
backgroundColor: '#F4F4F4',
},
listContainner: {
height: Dimensions.get('window').height,
marginBottom: 10
},
contentContainer: {
flexDirection: 'row',
width: width,
backgroundColor: 'white',
justifyContent: 'flex-start',
flexWrap: 'wrap'
},
letters: {
position: 'absolute',
height: height,
top: 0,
bottom: 0,
right: 10,
backgroundColor: 'transparent',
// justifyContent: 'flex-start',
// alignItems: 'flex-start'
alignItems: 'center',
justifyContent: 'center'
},
letter: {
height: height * 4 / 100,
width: width * 4 / 50,
justifyContent: 'center',
alignItems: 'center'
},
letterText: {
textAlign: 'center',
fontSize: height * 1.1 / 50,
color: '#e75404'
},
sectionView: {
paddingTop: 5,
paddingBottom: 5,
height: 30,
paddingLeft: 10,
width: width,
backgroundColor: '#F4F4F4'
},
sectionText: {
color: '#e75404',
fontWeight: 'bold'
},
rowView: {
height: ROWHEIGHT,
paddingLeft: 10,
paddingRight: 10,
borderBottomColor: '#F4F4F4',
borderBottomWidth: 0.5
},
rowdata: {
paddingTop: 10,
paddingBottom: 2
},
rowdatatext: {
color: 'gray',
width: width
},
rowViewBox: {
height: ROWHEIGHT_BOX,
width: (width - 30) / 3,
flexDirection: 'row',
backgroundColor: '#ffffff'
},
rowdataBox: {
borderWidth: 1,
borderColor: '#DBDBDB',
marginTop: 5,
marginBottom: 5,
paddingBottom: 2,
marginLeft: 10,
marginRight: 10,
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
rowDataTextBox: {
marginTop: 5,
flex: 1,
height: 20
}
});
然后在头部还需要实现一个搜索框。 SearchBox.js
'use strict';
import React, {Component} from 'react';
import {
View,
TextInput,
StyleSheet,
Platform,
} from 'react-native';
export default class SearchBox extends Component {
constructor(props) {
super(props);
this.state = {
value: ''
};
}
onEndEditingKeyword(vv) {
console.log(vv);
}
onChanegeTextKeyword(vv) {
console.log('onChanegeTextKeyword', vv);
this.setState({value: vv});
this.props.onChanegeTextKeyword(vv);
}
render() {
return (
<View style={styles.container}>
<View style={styles.inputBox}>
<View style={styles.inputIcon}>
</View>
<TextInput ref="keyword" autoCapitalize="none" value={this.props.keyword}
onChangeText={this.onChanegeTextKeyword.bind(this)} returnKeyType="search" maxLength={20}
style={styles.inputText} underlineColorAndroid="transparent"
placeholder={'输入城市名或拼音查询'}/>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
marginTop: 5,
marginBottom: 5,
backgroundColor: '#ffffff',
flexDirection: 'row',
height: Platform.OS === 'ios'
? 35
: 45,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#cdcdcd',
paddingBottom: 5
},
inputBox: {
height: Platform.OS === 'ios'
? 30
: 40,
marginLeft: 5,
marginRight: 5,
flex: 1,
flexDirection: 'row',
backgroundColor: '#E6E7E8'
},
inputIcon: {
margin: Platform.OS === 'ios'
? 5
: 10
},
inputText: {
alignSelf: 'flex-end',
marginTop: Platform.OS === 'ios'
? 0
: 0,
flex: 1,
height: Platform.OS === 'ios'
? 30
: 40,
marginLeft: 2,
marginRight: 5,
fontSize: 12,
lineHeight: 30,
textAlignVertical: 'bottom',
textDecorationLine: 'none'
}
});
最终效果:
最后是界面的绘制,这里就不多说了,大家可以下载源码自行查看。源码地址:http://download.csdn.net/detail/xiangzhihong8/9905924
- WordPress集成PHP缩略图,并开启Nginx缓存的方法
- 分享几个WordPress本地缓存gravatar评论头像的方案
- Linux下的mongodb服务脚本,以备不时之需
- WordPress发布文章主动推送到百度,加快收录保护原创
- Nginx开启fastcgi_cache缓存加速,支持html伪静态页面
- Go-指针、传值与传引用、垃圾回收
- SEO分享:彻底禁止搜索引擎抓取/收录动态页面或指定路径的方法
- Go-defer,panic,recover
- SEO技巧:Shell脚本自动提交网站404死链到搜索引擎
- Nginx发布1.9.0版本,新增支持TCP代理和负载均衡的stream模块
- WordPress4.2升级修复补丁:解决大量404请求以及评论表情路径及尺寸异常问题
- Linux系统编译安装Redis以及主从复制配置小记
- Go-Maps
- 为WordPress开启Nginx缩略图功能,七牛从此陌路
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 请不要再使用判断进行参数校验了
- 检测假新闻:比较不同的分类方法的准确率
- SpringBoot下载xlsx模板,导出excel数据
- 如何利用机器学习和Gatsby.js创建假新闻网站
- Spring Security 实战干货: 401和403状态
- Spring Data R2DBC响应式操作MySQL
- 项目端口可以设置为 6666 吗?
- 【JS】666- window.reqeustIdleCallback方法详解
- 一个无聊的宏定义
- windows的Ubuntu子系统ssh和ftp
- 面试官问:如何设计一个安全的对外接口?
- [树莓派公网远程监控]autossh反向代理
- 用 LDSC 计算遗传度以及遗传相关性
- SVG 入门指南(初学者入门必备)
- 快速介绍Python数据分析库pandas的基础知识和代码示例