[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 ...
随机推荐
- 建模语言UML
建模语言UML Unified Modeling Language (UML)又称统一建模语言或标准建模语言,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持, ...
- Beta冲刺(8/7)——2019.5.30
作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 队 ...
- i2c的读写时序图
根据实际示波器的波形画的时序图,时序图不好画,小小一幅图,画了两个小时,分享之:
- Installing Google Chrome in Linux(RedHat Enterprise Linux 7)
# wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm # yum -y install r ...
- C# 通过反射获取winform上的控件
比如获取Button按钮: System.Reflection.FieldInfo[] fieldInfo = form.GetType().GetFields(System.Reflection.B ...
- pyhanlp的安装
github 的官方地址:https://github.com/hankcs/pyhanlp conda install -c conda-forge jpype1 pip install pyhan ...
- riscv 汇编与反汇编
为了riscv指令集,我们需要汇编与反汇编工具来分析指令格式. 可以用下面的两个工具来汇编和反汇编,下载链接:https://pan.baidu.com/s/1eUbBlVc riscv-none-e ...
- APS中生产计划排程模块的基本原理
高级计划系统(APS)作为ERP和MES的补充,用于协调物流.开发瓶颈资源和保证交货日期. APS包括需求和供应计划.运输和生产计划排程等各种供应链计划模块,本文主要介绍APS中生产计划排程模块的基本 ...
- Android源码分析(十七)----init.rc文件添加脚本代码
一:init.rc文件修改 开机后运行一次: chmod 777 /system/bin/bt_config.sh service bt_config /system/bin/bt_config.sh ...
- provisional headers are shown 一例
系统首页的ajax调用出现 报错: provisional headers are shown 最后查到的原因时,mysql数据库的磁盘满了,而首页的ajax调用要插入一条记录到数据库,卡住了.