ReactNative SectionList實現分組折疊展開

ReactNative 0.43版本加入了FlatList、SectionList更好的替代ListView。
文檔我就不在這里拷貝了 自己先去看看 http://reactnative.cn/docs/0.47/flatlist.htmlhttp://reactnative.cn/docs/0.47/sectionlist.html

廢話不多說,先上個效果圖

sectionList.gif

接下來上代碼。

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

import SectionHeader from './SectionHeader'



export default class SectionListPage extends PureComponent {

    constructor(props){
        super(props);
        this.state = {
            //sectionList數據
            cellDataArray:[]
        };

        //源收據
        this.cellDatas = [
            {key:'1',title:'section1',show:true,data:[
                {key:'1',title:'row1'} ,
                {key:'2',title:'row2'} ,
                {key:'3',title:'row3'}
            ]
            },
            {key:'2',title:'section2',show:true,data:[
                {key:'4',title:'row1'} ,
                {key:'5',title:'row2'} ,
                {key:'6',title:'row3'}
            ]
            },
            {key:'3',title:'section3',show:true,data:[
                {key:'7',title:'row1'} ,
                {key:'8',title:'row2'} ,
                {key:'9',title:'row3'}
            ]
            },
            {key:'4',title:'section4',show:true,data:[
                {key:'10',title:'row1'} ,
                {key:'11',title:'row2'} ,
                {key:'12',title:'row3'}
            ]
            },

        ];

    }


    componentDidMount(){
        let newArray = JSON.parse(JSON.stringify(this.cellDatas));
        this.setState({
            cellDataArray:newArray
        })
    }


    handlerSectionHeader = (info) => {
        if (info.section.show) {
            this.state.cellDataArray.map((item, index) => {
                if (item === info.section) {
                    item.show = !item.show;
                    item.data = [{key:'close'}];
                }
            });

        }else {
            this.cellDatas.map((item,index) => {
                if (item.key === info.section.key){
                    let data = item.data;
                    this.state.cellDataArray.map((cellItem,i) => {
                        if (cellItem === info.section){
                            cellItem.show = !cellItem.show;
                            cellItem.data = data;
                        }
                    });
                }
            });

        }
        let newDatas= JSON.parse(JSON.stringify(this.state.cellDataArray));
        this.setState({
            cellDataArray:newDatas
        })
    };

    //sectionList頭部
    _ListHeaderComponent = () => {
        return (
            <View style={{height:35,backgroundColor:'#CD7F32',alignItems:'center',justifyContent:'center'}}>
                <Text>
                    SectionList Header
                </Text>
            </View>
        );

    };
    //sectionList底部
    _ListFooterComponent = () => {
        return (
            <View style={{height:35,backgroundColor:'#CD7F32',alignItems:'center',justifyContent:'center'}}>
                <Text>
                    SectionList Footer
                </Text>
            </View>

        );

    };

    //section之間的間隔
    _renderSectionSeparatorComponent = (info) => {
        return(
            <View style={{height:15,backgroundColor:'#9370DB'}}>

            </View>
        );
    };
    //cell之間的間隔
    _renderItemSeparatorComponent = (info) => {
        return(
            <View style={{height:1,backgroundColor:'blue'}}>

            </View>
        );

    };
    //section頭部
    _renderSectionHeader = (item) => {
        return (
            <SectionHeader
                info={item}
                handlerSectionHeader = {this.handlerSectionHeader.bind(this)}
            />
        );
    };

    //cell
    _renderItem = (info) => {

        //如果title為undefined (解決空數據section之間不顯示問題)
        if (info.item.title == undefined){
            return(
                <View>
                </View>
            );

        }else {
            return (
                <View style={{height:40,backgroundColor:'white',justifyContent:'center'}}>
                    <Text style={{color:'red'}}>
                        {info.item.title}
                    </Text>
                </View>
            );
        }
    };
    render() {
        return (
            <View style={styles.container}>
                <SectionList
                    ListHeaderComponent={this._ListHeaderComponent}
                    ListFooterComponent={this._ListFooterComponent}
                    SectionSeparatorComponent={this._renderSectionSeparatorComponent}
                    ItemSeparatorComponent={this._renderItemSeparatorComponent}
                    renderSectionHeader={this._renderSectionHeader}
                    renderItem={this._renderItem}
                    sections={this.state.cellDataArray}
                    extraData={this.state}
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,

    }
});

基本用法:

  <SectionList
                    ListHeaderComponent={this._ListHeaderComponent}
                    ListFooterComponent={this._ListFooterComponent}
                    SectionSeparatorComponent={this._renderSectionSeparatorComponent}
                    ItemSeparatorComponent={this._renderItemSeparatorComponent}
                    renderSectionHeader={this._renderSectionHeader}
                    renderItem={this._renderItem}
                    sections={this.state.cellDataArray}
                    extraData={this.state}
                />

特別注意的地方:
如需用setState來更新頁面 需要設置:

extraData={this.state}

另外1.數據中需要有data:才能讀取數據

 {key:'1',title:'section1',show:true,data:[
                {key:'1',title:'row1'} ,
                {key:'2',title:'row2'} ,
                {key:'3',title:'row3'}
            ]}

section和item都需要key,如果不想手動增加key則可以用

keyExtractor: (item: Item, index: number) => string

自動生成。

有什么不對的請大家指出~謝謝。
完整Demo
https://github.com/RoarRain/react-native-SectionList

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容