React Native實現(xiàn)城市選擇列表,仿聯(lián)系人列表

項目地址源碼地址點我
如果有不懂的問題,可以在評論區(qū)留言, 看到了就回復(fù),每天都能回復(fù)

城市選擇預(yù)覽


'use strict';
import React from "react"
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Dimensions,
    SectionList,
    FileList,
    ListView
} from 'react-native';
const { width, height } = Dimensions.get('window');
// 適配性函數(shù)
const UIWIDTH = 750
export function rx(UIPX) {
    return Math.round(UIPX * width / UIWIDTH);
}
import cityIndex from "./cityIndex"
// 字母的高度
const CONTENT_LIST_INDEX = rx(60)
// 每一個城市的高度
const CONTENT_LIST_TAG = rx(80)
export default class CityList extends React.Component {
    constructor(props) {
        super(props);
        this.dSource = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
        this.sectionList = null
        this.dealCity()
    }
    componentDidMount = () => {
        this.initPage()
    }
    // 此頁面初始化
    initPage = () => {
        this.sectionList = this.refs.sectionList

    }
    //  二次處理數(shù)據(jù)
    dealCity = () => {
        // 在最前面追加數(shù)據(jù)
        let p = cityIndex
        // p.unshift({ sortLetters: "最近", data: [{ name: "鄭州1" }, { name: "北京2" }, { "name": "上海3" }] })
        p.unshift({ sortLetters: "定位", data: [{ name: "鄭州" }] })
    }
    _renderSectionHeader = ({ section }) => {
        return (<View style={styles.contentListIndex}>
            <Text style={styles.contentListIndexText}>{section["sortLetters"]}</Text>
        </View>)

    }
    _renderItem = ({ item, section }) => {
        if (section["sortLetters"] == "定位") {
            return (<TouchableOpacity onPress={() => {
                this.selectCity(item)
            }}><View style={[styles.contentListTag, styles.contentListTagMap]}>
                    <View style={[styles.contentListTagTextMap]}>
                        <Text style={styles.contentListTagTextMapIn}>鄭州</Text>
                    </View>
                </View></TouchableOpacity>)
        } else {
            return (<TouchableOpacity onPress={() => {
                this.selectCity(item)
            }}><View style={styles.contentListTag}>
                    <Text style={styles.contentListTagText}>{item["name"]}</Text>
                </View></TouchableOpacity>)
        }
    }

    _renderHistory = () => {
        return (<View style={[styles.contentListTag, styles.contentListTagMap]}>
            {section["data"].map(itemIn => {
                return (<TouchableOpacity><View style={[styles.contentListTagTextMap]}>
                    <Text style={styles.contentListTagTextMapIn}>{itemIn["name"]}</Text>
                </View></TouchableOpacity>)
            })}
        </View>)
    }
    _scrollTo = (index) => {
        // console.log(index)
        this.sectionList.scrollToLocation({
            animated: true,
            sectionIndex: index,
            itemIndex: 0,
            // 補償高度偏移
            viewOffset: CONTENT_LIST_TAG * 4,
            // viewOffset:0
        })
    }
    selectCity = (cityItem) => {
        console.log(cityItem)
    }
    render = () => {
        // 右側(cè)列表
        let rightIndex = [...cityIndex.map((item, index) => {
            return (<TouchableOpacity key={item["sortLetters"]} onPress={() => {

                this._scrollTo(index)
            }}><View style={styles.indexBox}>
                    <Text style={styles.indexText}>{item["sortLetters"]}</Text>
                </View></TouchableOpacity>)
        })]
        return (<View style={styles.page}>
            <View style={styles.container}>
                {/** 1.選擇當(dāng)前的定位*/}
                {/** 2.渲染最近打開的地理位置*/}
                {/** 3.渲染整個列表*/}
                <View style={styles.contentContainer}>
                    <View style={styles.contentList}>
                        <SectionList
                            ref="sectionList"
                            stickySectionHeadersEnabled={true}
                            sections={cityIndex}
                            renderSectionHeader={this._renderSectionHeader}
                            renderItem={this._renderItem}
                            keyExtractor={(item, index) => "" + index}
                            getItemLayout={(data, index) => {
                                return {
                                    index,
                                    // 偏移高度 = 每一個城市(CONTENT_LIST_TAG)x數(shù)量+字母高度(CONTENT_LIST_INDEX)
                                    offset: CONTENT_LIST_TAG * (index) + CONTENT_LIST_INDEX,
                                    length: CONTENT_LIST_TAG
                                }
                            }}
                        ></SectionList>
                    </View>
                </View>
                <View style={styles.rightIndex}>
                    {rightIndex}
                </View>
            </View>
        </View>)
    }
}
const styles = StyleSheet.create({
    page: {
        flex: 1,
        height,
        width,
        backgroundColor: "#f5f5f5"
    },
    // 中間內(nèi)容
    container: {
        flex: 1,
        flexDirection: "column",
        backgroundColor: "#fff"
    },

    contentList: {
        backgroundColor: "#fff",
    },
    // 1.選擇當(dāng)前的定位
    // 定位的樣式
    // 2.渲染最近打開的地理位置
    contentListTagMap: {
        height: CONTENT_LIST_TAG,
        width,
        paddingHorizontal: rx(20),
        backgroundColor: "#f5f5f5",
        flexDirection: "row",
        alignItems: "center",
    },
    contentListTagTextMap: {
        height: CONTENT_LIST_TAG,
        width: (width - 30) / 3,
        backgroundColor: "#f5f5f5",
    },
    contentListTagTextMapIn: {
        lineHeight: CONTENT_LIST_TAG - rx(30),
        height: CONTENT_LIST_TAG - rx(30),
        width: (width - 40) / 3,
        textAlign: "center",
        backgroundColor: "#fff",
        color: "#999",
        fontWeight: "bold",
        borderRadius: rx(10),
        borderWidth: 1,
        borderColor: "#ddd",
    },
    // 3.渲染整個列表
    contentListIndex: {
        height: CONTENT_LIST_INDEX,
        width,
        paddingHorizontal: rx(20),
        backgroundColor: "#f5f5f5",
    },
    contentListIndexText: {
        height: CONTENT_LIST_INDEX,
        width,
        lineHeight: CONTENT_LIST_INDEX,
        fontSize: rx(36),
        color: "#f60",
        fontFamily: "iconfont",
    },
    contentListTag: {
        height: CONTENT_LIST_TAG,
        width,
        paddingHorizontal: rx(20),
        backgroundColor: "#fff",
    },
    contentListTagText: {
        height: CONTENT_LIST_TAG,
        width,
        lineHeight: CONTENT_LIST_TAG,
        color: "#999",
        fontWeight: "bold"
    },
    // 右側(cè)
    rightIndex: {
        position: "absolute",
        right: rx(10),
        top: rx(20),
        bottom: 0,
        height,
        backgroundColor: "#fff",
        alignItems: "center"
    },
    indexBox: {
        height: rx(50),
        backgroundColor: "#fff",
    },
    indexText: {
        fontSize: rx(30),
        color: "#f60",
        fontWeight: "bold"
    }
});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 花千骨說,白子畫是這世上最溫柔的人,也是這世上最無情的人。可以這么說,但又不全是。不論后句,單論前句,我就對...
    靈芙醉客閱讀 382評論 2 1
  • 另謀高就 劇情:瑞秋厭倦了每天端咖啡的工作,提出辭職,正式進軍時尚界;羅斯不小心把小女孩砸傷,為了完成小女孩的太空...
    小神2閱讀 1,867評論 0 2

友情鏈接更多精彩內(nèi)容