react native 分组列表

本文通过实例详细介绍了React Native中SectionList组件的使用方法,包括数据绑定、分组展示及行间分隔线等特性,并对比了其与FlatList的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用官网:如果你的列表不需要分组(section),那么可以使用结构更简单的<FlatList>

import React, {Component} from 'react';
import {
    Text,
    View,
    SectionList,
    StyleSheet,
} from 'react-native'


import Dimensions from 'Dimensions'
const { width,height } = Dimensions.get('window')

let data = [
    {
        "key": "A",
        "data": [
            {
                "title": "正许六",
                "name": "阿多标"
            },
            {
                "title": "于南装",
                "name": "阿人院"
            },
            {
                "title": "准象白",
                "name": "阿照速"
            }
        ],
    },
    {
        "key": "B",
        "data": [
            {
                "title": "切规指",
                "name": "白低族"
            },
            {
                "title": "海对导",
                "name": "白义局"
            },
            {
                "title": "切历联",
                "name": "白北选"
            }
        ],
    },
    {
        "key": "C",
        "data": [
            {
                "title": "三放类",
                "name": "陈分革"
            },
            {
                "title": "关党部",
                "name": "陈然传"
            },
            {
                "title": "京需运",
                "name": "陈土务"
            }
        ],
    }
  ]

export default class List extends Component {
    constructor(props){
        super(props);
        this.state = {
            data: data
        }
    }
    render() {
        return (
            <View style={styles.container}>
                <SectionList
                    sections={this.state.data} //数据源,类似于<FlatList>中的data属性
                    renderSectionHeader={this._sectionComp} //每个section的头部渲染
                    renderItem={this._renderItem} //渲染每一个section中的每一个列表项
                    keyExtractor={(item,index) => index.toString()} //为给定的item生成一个不重复的key
                    ItemSeparatorComponent={this._itemSeparator} //行与行之间的分隔线
                    SectionSeparatorComponent={this._sectionSeparator} //在每个`section`的顶部和底部渲染
                    stickySectionHeadersEnabled={false} //是否吸顶,ios默认为true;Android默认为false
                />  
            </View>
        );
    }
    _sectionComp = (item) => {
        return(
            <Text style={styles.key}>{item.section.key}</Text>
        )
    }
    _renderItem = (item) => {
        return(
            <Text style={styles.name}>{item.item.name}</Text>
        )
    }
    _itemSeparator = () => {
        return(
            <Text style={styles.separator}></Text>
        )
    }
    _sectionSeparator = () => {
        return(
            <Text style={[styles.separator,{backgroundColor:'#f00'}]}></Text>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    key: {
        width: width,
        height: 50,
        backgroundColor: 'cyan',
        lineHeight: 50,
        textAlign: 'center',
    },
    name: {
        width: width,
        height: 35,
        backgroundColor: '#fff',
        lineHeight: 35,
        paddingLeft: 20,
    },
    separator: {
        height: 2,
    }
})

效果:stickySectionHeadersEnabled为false:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值