[RN] React Native 让 Flatlist 支持 选中多个值,并获取所选择的值
React Native 让 Flatlist 支持 选中多个值,并获取所选择的值
实现效果如下:

实现代码:
import React, {Component} from 'react';
import {
    Image,
    Text,
    View,
    TouchableOpacity,
    FlatList,
    StyleSheet,
    Button
} from 'react-native';
export default class TestListCheck extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [
                {
                    "id": "",
                    select: false
                },
                {
                    "id": "",
                    select: false
                },
                {
                    "id": "",
                    select: false
                },
                {
                    "id": "",
                    select: false
                },
                {
                    "id": "",
                    select: false
                },
                {
                    "id": "",
                    select: false
                }
            ],//数据源
            selectItem: [],
        }
    }
    _selectItemPress(item) {
        if (item.select) {
            this.state.selectItem.splice(this.state.selectItem.findIndex(function (x) {
                return x === item.id;
            }), );
        } else {
            this.state.selectItem.push(item.id);
        }
        this.state.data[item.id].select = !item.select;
        this.setState({data: this.state.data})
    }
    _submitPress() {
        alert(`选中了${JSON.stringify(this.state.selectItem)}`)
    }
    render() {
        return (
            <FlatList
                keyExtractor={item => item.id}
                data={this.state.data}
                extraData={this.state} //这里是关键,如果不设置点击就不会马上改变状态,而需要拖动列表才会改变
                ListHeaderComponent={({item}) => {
                    return (<Button title={"确定"} onPress={() => this._submitPress()}/>)
                }}
                renderItem={({item}) => {
                    return (
                        <View style={styles.standaloneRowFront}>
                            <TouchableOpacity
                                onPress={() => this._selectItemPress(item)}>
                                <View style={styles.row}>
                                    {item.select ?
                                        <Image source={require('./ic_radio_checkbox_check.png')}
                                               style={styles.imgCheckIcon}/>
                                        :
                                        <Image source={require('./ic_radio_checkbox_uncheck.png')}
                                               style={styles.imgCheckIcon}/>
                                    }
                                    <View style={{marginLeft: }}>
                                        <Text>{item.select ? ("选中") : ("未选中")}</Text>
                                    </View>
                                </View>
                            </TouchableOpacity>
                        </View>
                    )
                }}
            />
        );
    }
}
const styles = StyleSheet.create({
    standaloneRowFront: {
        flexDirection: 'row',
        alignItems: 'center',
        backgroundColor: '#FFF',
        height: ,
        marginBottom:
    },
    imgCheckIcon: {
        width: ,
        height: ,
        lineHeight: ,
    },
    row: {
        flexDirection: "row",
        alignItems: "center",
    },
});
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/11080603.html
转载请著名出处!谢谢~~
[RN] React Native 让 Flatlist 支持 选中多个值,并获取所选择的值的更多相关文章
- [RN] React Native 使用 FlatList 和 ScrollView 的下拉刷新问题
		
React Native 使用 FlatList 和 ScrollView 实现 下拉刷新时,RefreshControl 控件不起作用, 后来经查明,原来 RefreshControl 要加在 Sc ...
 - [RN] React Native 使用 FlatList 实现九宫格布局 GridList
		
React Native 使用 FlatList 实现九宫格布局 先看图片演示实例: 本文以图片列表为例,实现九宫格布局! 主要有两种方法: 1)方法一: 利用FlatList的 numColumns ...
 - [RN] React Native 实现 FlatList上拉加载
		
FlatList可以利用官方组件 RefreshControl实现下拉刷新功能,但官方没有提供相应的上拉加载的组件,因此在RN中实现上拉加载比下拉刷新要复杂一点. 不过我们仍可以通过FlatList ...
 - [RN] React Native 下实现底部标签(支持滑动切换)
		
上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...
 - React Native之FlatList的介绍与使用实例
		
React Native之FlatList的介绍与使用实例 功能简介 FlatList高性能的简单列表组件,支持下面这些常用的功能: 完全跨平台. 支持水平布局模式. 行组件显示或隐藏时可配置回调事件 ...
 - [RN] React Native  常见基本问题归纳总结
		
[RN] React Native 常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...
 - [RN] React Native 实现图片预览
		
[RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...
 - [RN] React Native 关闭所有黄色警告
		
[RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...
 - [RN] React Native 幻灯片效果 Banner
		
[RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...
 
随机推荐
- idea类存在找不到解决办法
			
清除idea缓存,
 - 集合类源码(七)Map(ConcurrentHashMap, ConcurrentSkipListMap, TreeMap)
			
ConcurrentHashMap 内部结构 在JDK1.8之前的实现结构是:ReentrantLock+Segment+HashEntry+链表 JDK1.8之后的实现结构是:synchronize ...
 - java中的泛型【T】与通配符【?】概念入门
			
使用泛型的目的是利用Java编译机制,在编译过程中帮我们检测代码中不规范的有可能导致程序错误的代码.例如,我们都知道List容器可以持有任何类型的数据,所以我们可以把String和Integer等类型 ...
 - Password file not found:.../jmxremote.password
			
jmxremote.password 在jdk/jre/lib/management/下,jmxremote.password.template复制,去掉.template后缀 在配置JMX远程访问的 ...
 - excel中使用统计列中的值在其他列出现的次数
			
excel中使用统计一列的中值在其他列出现的次数 =COUNTIFS($J$:$J$,K2) 解释下 $J$2 J列中的第二行到 $J$373 J列的373行 范围内 查找 k列的第二行的值 出现的 ...
 - FileChannel(API详解)
			
1.两种获取通道的方法FileChannel.open()的方式 FileChannel channell = FileChannel.open(Paths.get("a.txt" ...
 - SpringBoot 打包成war
			
1.修改pom.xml文件 <packaging>war</packaging> <properties> <project.build.sourceEnco ...
 - 模板模式创建一个poi导出功能
			
之前的导出都很乱,直接写在代码中,等到下回还使用导出功能时又不知如何下手,今天用模板模式重写了一个导出功能,方便以后使用: package com.sf.addrCheck.util.export.p ...
 - Flask简介及使用
			
目录 Flask简介 wsgiref wsgiref简单应用 两个依赖 werkzeug Jinja2 简单使用 安装 flask快速使用 Django与Flask返回值的对比 Flask简介  F ...
 - ELK——Elasticsearch 搭建集群经验
			
本文内容 背景 ES集群中第一个master节点 ES slave节点 迁移到:http://www.bdata-cap.com/newsinfo/1712679.html 本文总结 Elastics ...