惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
SegmentFault 最新的问题
Jina AI
Jina AI
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
B
Blog
博客园 - 【当耐特】
博客园 - Franky
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta

博客园 - 斌言

Claude Code + deepseek在Windows环境上的搭建教程 docker部署.NET6并挂载目录 unable to access 'https://github.com/langgenius/dify.git/': Failed to connect to github.com port 443 after 21111 ms: Could not connect to server .NET6 MVC 无刷新上传文件 docker快速搭建部署mqtt ubuntu安装smaba CentOS7.9安装Docker centos7.9 安装 openGauss 5.0.0 CentOS 7 yum安装MongoDB CentOS 7.4安装.NET6 react-native-vector-icons 安装 apk文件查看指纹证书方法 server2008 安装mongodb supervisor 安装及基本使用 WinForms 嵌入 Web服务 ASP.NET Core http请求内容过大, IIS服务器 返回 Request Too Long 解决方案 ASP.NET Core MVC中调用Json()时默认使用Newtonsoft.Json返回数据 react native android9 axios network error .NET Core3.1升级.NET5 oracle连接报错
react native SectionList组件实现多选
斌言 · 2022-11-30 · via 博客园 - 斌言
import React, { useRef, Component } from 'react';
import {
    Platform,
    Text,
    View,
    TextInput,
    TouchableOpacity,
    ScrollView,
    Image,
    Button,
    SectionList,
    StyleSheet,
    ToastAndroid,
    Dimensions,
    Alert,
} from 'react-native';

import Icon from 'react-native-vector-icons/Ionicons';

   const DATA = [

    {
        titleId: "1",
        titleName: "水果",
        data: [
            { id: '01', name: '香蕉', selected: false },
            { id: '02', name: '', selected: false },
            { id: '03', name: '葡萄', selected: false },
            { id: '04', name: '猕猴桃', selected: false },
            { id: '05', name: '苹果', selected: false },
            { id: '06', name: '桃子', selected: false },
            { id: '07', name: '西瓜', selected: false },
            { id: '08', name: '橘子', selected: false },
        ]
    },
    {
        titleId: "2",
        titleName: "菜品",
        data: [
            { id: '09', name: '辣椒', selected: false },
            { id: '10', name: '白菜', selected: false },
            { id: '11', name: '青菜', selected: false },
            { id: '12', name: '茄子', selected: false },
            { id: '13', name: '南瓜', selected: false },
            { id: '14', name: '土豆', selected: false },
            { id: '15', name: '西红柿', selected: false },
            { id: '16', name: '粉条', selected: false },
            { id: '17', name: '豇豆', selected: false },
            { id: '18', name: '牛肉', selected: false },
            { id: '19', name: '猪肉', selected: false },
            { id: '20', name: '鸡翅', selected: false },
            { id: '21', name: '鸡爪', selected: false },
            { id: '22', name: '鸭肉', selected: false },

        ]
    },

];

export default class TestScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            sourceData: DATA,
            selectedItem: []//选中的项
        }
    };

    render() {return (
            <View style={styles.container}><SectionList
                        sections={this.state.sourceData}
                        keyExtractor={(item, index) => index.toString()}
                        extraData={this.state}
                        stickySectionHeadersEnabled={true}//吸顶效果
                        renderItem={this._renderItem}   //cell
                        renderSectionHeader={({ section: { titleName } }) => (
                            <View style={{ height: 40, justifyContent: 'center', backgroundColor: 'rgba(232,240,248,1)' }}>
                                <Text style={[, { color: "#0a3989", textAlign: 'center', fontSize: CommonVar.userStyle.titleFontSize + 2 }]}>{titleName}</Text>
                            </View>
                        )}
                        ItemSeparatorComponent={() => {
                            return <View style={{ borderWidth: 0.2, borderColor: "#d2d2d2" }} />
                        }}
                    />
            </View>
        )
    }

    _renderItem = (info) => {
        // console.log(info);
        if (info.item.selected == true) {
            return <TouchableOpacity onPress={this._itemPress.bind(this, info.item, info.index)}>
                <View style={{ height: 45, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#FFFFFF' }}>
                    <Text style={{ marginLeft: 10, alignSelf: 'center', color: "#000000" }}>{info.item.name}</Text>
                    <Icon name="ios-checkmark-outline" color='blue' size={25} style={{ alignSelf: 'center', marginRight: 5 }} />
                </View>
            </TouchableOpacity>
        } else {
            return <TouchableOpacity onPress={this._itemPress.bind(this, info.item, info.index)}>
                <View style={{ height: 45, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#FFFFFF' }}>
                    <Text style={{ marginLeft: 10, alignSelf: 'center', color: "#000000" }}>{info.item.name}</Text>
                    <Icon name="ios-square-outline" color='#d2d2d2' size={25} style={{ alignSelf: 'center', marginRight: 5 }} />
                </View>
            </TouchableOpacity>
        }
    }

    _itemPress(selectItem, index) {
        var $this = this;
        this.state.sourceData.forEach(function (item1, lev1Index) {
            item1.data.forEach(function (item2, lev2Index) {
                if (item2.id == selectItem.id) {
                    //循环数据是否存在,存在就移除
                    var isExist = false;
                    $this.state.selectedItem.forEach(function (obj, objIndex) {
                        if (obj.id == selectItem.id && obj.titleId == item1.titleId) {
                            //找到存在的对象删除掉
                            $this.state.selectedItem.splice(objIndex, 1);
                            isExist = true;
                        }
                    })
                    if (isExist == false) {
                        //不存在就加到集合中去
                        $this.state.selectedItem.push({ id: selectItem.id, titleId: item1.titleId });
                    }
                    $this.state.sourceData[lev1Index].data[index].selected = !selectItem.selected;
                }
            })
        })
        console.log(this.state.selectedItem)
        this.setState({ sourceData: this.state.sourceData })
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: "#FFFFFF"
    },
   

})