• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

    react native帶索引的城市列表組件的實例代碼

    來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:33:14
    文檔

    react native帶索引的城市列表組件的實例代碼

    react native帶索引的城市列表組件的實例代碼:城市列表選擇是很多app共有的功能,比如典型的美圖app。那么對于React Native怎么實現(xiàn)呢? 要實現(xiàn)上面的效果,首先需要對界面的組成簡單分析,界面的數(shù)據(jù)主要由當(dāng)前城市,歷史訪問城市和熱門城市組成,所以我們在提供Json數(shù)據(jù)的時候就需要將數(shù)據(jù)分為至少3
    推薦度:
    導(dǎo)讀react native帶索引的城市列表組件的實例代碼:城市列表選擇是很多app共有的功能,比如典型的美圖app。那么對于React Native怎么實現(xiàn)呢? 要實現(xiàn)上面的效果,首先需要對界面的組成簡單分析,界面的數(shù)據(jù)主要由當(dāng)前城市,歷史訪問城市和熱門城市組成,所以我們在提供Json數(shù)據(jù)的時候就需要將數(shù)據(jù)分為至少3

    城市列表選擇是很多app共有的功能,比如典型的美圖app。那么對于React Native怎么實現(xiàn)呢?

    這里寫圖片描述

    要實現(xiàn)上面的效果,首先需要對界面的組成簡單分析,界面的數(shù)據(jù)主要由當(dāng)前城市,歷史訪問城市和熱門城市組成,所以我們在提供Json數(shù)據(jù)的時候就需要將數(shù)據(jù)分為至少3部分。

    const ALL_CITY_LIST = DATA_JSON.allCityList;
    const HOT_CITY_LIST = DATA_JSON.hotCityList;
    const LAST_VISIT_CITY_LIST = DATA_JSON.lastVisitCityList;

    而要實現(xiàn)字母索引功能,我們需要自定義一個控件,實現(xiàn)和數(shù)據(jù)的綁定關(guān)系,自定義組件代碼如下:

    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 = []; //每個字母對應(yīng)的城市和字母的總高度
    
    const {width, height} = Dimensions.get('window');
    
    var that;
    
    const key_now = '當(dāng)前';
    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
     }
    
    });
     
    
    

    然后在頭部還需要實現(xiàn)一個搜索框。

    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'
     }
    });
    
    

    最終效果:

    這里寫圖片描述 

    這里寫圖片描述 

    最后是界面的繪制,這里就不多說了,大家可以下載源碼自行查看。源碼地址:react-native-city_jb51.rar

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    react native帶索引的城市列表組件的實例代碼

    react native帶索引的城市列表組件的實例代碼:城市列表選擇是很多app共有的功能,比如典型的美圖app。那么對于React Native怎么實現(xiàn)呢? 要實現(xiàn)上面的效果,首先需要對界面的組成簡單分析,界面的數(shù)據(jù)主要由當(dāng)前城市,歷史訪問城市和熱門城市組成,所以我們在提供Json數(shù)據(jù)的時候就需要將數(shù)據(jù)分為至少3
    推薦度:
    標(biāo)簽: 城市 re 的代碼
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产精品热久久毛片| 国产成人精品日本亚洲专区| 日本精品卡一卡2卡3卡四卡| 亚洲综合国产精品第一页 | 国产欧美精品AAAAAA片| 久久久久久久久久免免费精品| 久久国产精品久久久| 国产人妖乱国产精品人妖| 亚洲精品无码久久久久去q| 毛片a精品**国产| 国产亚洲精品激情都市| 桃花岛精品亚洲国产成人| 国产AV午夜精品一区二区入口| 亚洲国产精品无码久久一线| 免费国产在线精品一区| 国内精品久久久久久久久电影网 | 精品无码久久久久久久久久| 99精品欧美一区二区三区| 国产精品视频网站你懂得| 97久久久精品综合88久久| 久久精品aⅴ无码中文字字幕重口| 亚洲精品专区在线观看| 久久99精品免费一区二区| 国产精品高清免费网站| 91午夜精品亚洲一区二区三区| 四虎成人精品免费影院| 精品免费视在线观看| 国产成人精品男人的天堂538| 国产精品三级在线观看无码| 精品免费人成视频app| 精品国产三级a∨在线| 精品亚洲成a人片在线观看少妇| 久久久久亚洲精品无码蜜桃| 婷婷五月深深久久精品| 精品偷自拍另类在线观看| 国产人妖乱国产精品人妖| 91麻豆精品视频在线观看| 久久er热视频在这里精品| 在线精品国产一区二区| 国产精品免费观看视频| 国产精品无码久久四虎|