一、看图说话

二、源码

【git】https://gitee.com/naak2162/MySelectAll.git

【csdn不知道怎么设置免费下载】https://download.csdn.net/download/sinat_15411661/11141708

三、正文

1、关于临时json数据显示:assets文件夹内放置json资源文件

2、关于字符串操作的工具包:将string类型、integer类型的集合List、List转换成string字符串

3、封装Gson序列化工具类:

(1)在工程目录中build.gradle的dependencies中加入Gson的三方网络支持框架

 implementation 'com.google.code.gson:gson:2.8.2'

(2)简易封装Gson的Object解析、 List<T>解析、序列化工具类

public class GsonUtils {private static Gson gson = new Gson();public static <T> T parseJson(String jsonData, Class<T> type) {T result = gson.fromJson(jsonData, type);return result;}public static <T> List<T> parseJsonArray(String jsonData, Class<T> clazz) {List<T> result = new ArrayList<T>();if (!TextUtils.isEmpty(jsonData)) {try {JsonArray array = new JsonParser().parse(jsonData).getAsJsonArray();if (array != null) {for (JsonElement elem : array) {result.add(gson.fromJson(elem, clazz));}}} catch (JsonSyntaxException e) {Log.e("GsonUtils", "parseJsonArray error" + e);}}return result;}public static String toJson(Object object){return gson.toJson(object);}
}

4、源码解析:

(1)需要解析的json数据

[{"name": "筷" },{"name": "碗" },{ "name": "碟" }, {"name": "杯"},{"name": "勺"}]

(2)创建相应的bean类,设置标识isCheck

public class CleanGoods {/*** name : 筷*/public String name;public boolean isCheck;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

(3)创建activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"android:orientation="vertical"><android.support.v7.widget.RecyclerViewandroid:id="@+id/recycleView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="11dp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="38dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btnYes"android:layout_width="match_parent"android:layout_height="42dp"android:layout_margin="12dp"android:layout_weight="1"android:background="@drawable/btn_bg"android:text="全选"android:textColor="@android:color/white"android:textSize="17sp" /><Buttonandroid:id="@+id/btnNo"android:layout_width="match_parent"android:layout_height="42dp"android:layout_margin="12dp"android:layout_weight="1"android:background="@drawable/btn_bg"android:text="全不选"android:textColor="@android:color/white"android:textSize="17sp" /><Buttonandroid:id="@+id/btnCommit"android:layout_width="match_parent"android:layout_height="42dp"android:layout_margin="12dp"android:layout_weight="1"android:background="@drawable/btn_bg"android:text="确  定"android:textColor="@android:color/white"android:textSize="17sp" /></LinearLayout><TextViewandroid:id="@+id/tvContent"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="26dp"android:hint="点击【确定】按钮显示文本"android:textColor="@color/colorPrimary"android:textColorHint="@android:color/darker_gray"android:textSize="18sp"android:textStyle="bold"/>
</LinearLayout>

(4)其中(3)的自定义drawable下的btn_bg.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><!-- 设置边框 --><solid android:color="@color/colorPrimary" /><!-- 填充的颜色 --><!-- 指定圆角矩形的4个圆角的半径 --><cornersandroid:bottomLeftRadius="3dp"android:bottomRightRadius="3dp"android:topLeftRadius="3dp"android:topRightRadius="3dp" />
</shape>

(5)创建适配器SelectGoodsAdapter.java文件

class SelectGoodsAdapter extends RecyclerView.Adapter<SelectGoodsAdapter.SelectGoodsHolder> {private List<CleanGoods> mData;private Context mContext;private LayoutInflater mInflater;private List<String> nameIdList = new ArrayList<>();SelectGoodsAdapter(Context context, List<CleanGoods> list) {this.mContext = context;mInflater = LayoutInflater.from(mContext);this.mData = new ArrayList<>();this.mData.addAll(list);}@SuppressLint("SetTextI18n")private void notifyData(List<CleanGoods> list) {if (list != null) {this.mData.clear();this.mData.addAll(list);}notifyDataSetChanged();for (int i = 0; i < mData.size(); i++) {if (mData.get(i).isCheck) {nameIdList.add(mData.get(i).getName());} else {nameIdList.remove(mData.get(i).getName());}}List<String> tempImages = removeDuplicate(nameIdList);}private List<String> removeDuplicate(List<String> list) {Set set = new LinkedHashSet<String>(list);list.clear();list.addAll(set);return list;}@NonNull@Overridepublic SelectGoodsHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = mInflater.inflate(R.layout.item_my_select, parent, false);SelectGoodsHolder holder = new SelectGoodsHolder(view);return holder;}@Overridepublic void onBindViewHolder(SelectGoodsHolder holder, int position) {final CleanGoods dataInfo = mData.get(position);holder.tvItem.setText(dataInfo.getName());final boolean isCheck = dataInfo.isCheck;if (isCheck) {holder.ivCheck.setImageResource(R.mipmap.image_check);} else {holder.ivCheck.setImageResource(R.mipmap.image_uncheck);}holder.llLayout.setOnClickListener(v -> {String newId = dataInfo.getName();for (CleanGoods dataInfo1 : datas) {String oldId = dataInfo1.name;if (newId.equals(oldId)) {boolean isCheck1 = dataInfo1.isCheck;if (isCheck1) {dataInfo1.isCheck = false;} else {dataInfo1.isCheck = true;setCheck(dataInfo);}break;}}notifyData(datas);});}public void setCheck(CleanGoods info) {if (checkNow.get(info.getName()) == null) {checkNow.put(info.getName(), info);} else {checkNow.remove(info.getName());}}@Overridepublic int getItemCount() {return mData.size();}class SelectGoodsHolder extends RecyclerView.ViewHolder {ImageView ivCheck;TextView tvItem;LinearLayout llLayout;SelectGoodsHolder(View itemView) {super(itemView);ivCheck = (ImageView) itemView.findViewById(R.id.ivCheck);tvItem = (TextView) itemView.findViewById(R.id.tvName);llLayout = (LinearLayout) itemView.findViewById(R.id.llLayout);}}}

设置被选中数据和移除数据:

public void setCheck(CleanGoods info) {if (checkNow.get(info.getName()) == null) {checkNow.put(info.getName(), info);} else {checkNow.remove(info.getName());}}

将选中的list数据按照顺序排放且不重复数据:

 private List<String> removeDuplicate(List<String> list) {Set set = new LinkedHashSet<String>(list);list.clear();list.addAll(set);return list;}

被选中数据或者取消被选中数据刷新recycleview:

@SuppressLint("SetTextI18n")private void notifyData(List<CleanGoods> list) {if (list != null) {this.mData.clear();this.mData.addAll(list);}notifyDataSetChanged();for (int i = 0; i < mData.size(); i++) {if (mData.get(i).isCheck) {nameIdList.add(mData.get(i).getName());} else {nameIdList.remove(mData.get(i).getName());}}List<String> tempImages = removeDuplicate(nameIdList);}

(6)创建item_my_select.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:card_view="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="3dp"android:layout_marginRight="3dp"android:layout_marginBottom="1dp"android:background="@android:color/white"android:foreground="?android:attr/selectableItemBackground"android:gravity="center"card_view:cardBackgroundColor="@android:color/white"card_view:cardCornerRadius="4dp"card_view:cardUseCompatPadding="true"><LinearLayoutandroid:id="@+id/llLayout"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="11dp"><ImageViewandroid:id="@+id/ivCheck"android:layout_width="30dp"android:layout_height="30dp"android:layout_gravity="center_vertical"android:layout_marginLeft="15dp"android:src="@mipmap/image_uncheck" /><TextViewandroid:id="@+id/tvName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginLeft="16dp"android:paddingTop="8dp"android:paddingRight="15dp"android:paddingBottom="8dp"android:text="食品生产经营者"android:textColor="@android:color/black"android:textSize="16sp" /></LinearLayout>
</android.support.v7.widget.CardView>

(7)创建适配器MainActivity.java文件,继承AppCompatActivity

public class MainActivity extends AppCompatActivity {@BindView(R.id.recycleView)RecyclerView recycleView;@BindView(R.id.tvContent)TextView tvContent;private List<CleanGoods> datas;private SelectGoodsAdapter adapter;private Map<String, CleanGoods> checkNow = new HashMap<String, CleanGoods>();private List<String> goodsName = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);initData();}private void initData() {datas = new ArrayList<>();String jsonData = KitIOUtil.getStringFromAssets(this, "goods.json");datas = GsonUtils.parseJsonArray(jsonData, CleanGoods.class);recycleView.setLayoutManager(new LinearLayoutManager(this));adapter = new SelectGoodsAdapter(this, datas);recycleView.setAdapter(adapter);}@OnClick({R.id.btnYes, R.id.btnNo, R.id.btnCommit})public void onViewClicked(View view) {switch (view.getId()) {case R.id.btnYes:setData(true);break;case R.id.btnNo:setData(false);break;case R.id.btnCommit:if (adapter != null) {for (int i = 0; i < datas.size(); i++) {if (datas.get(i).isCheck) {goodsName.add(datas.get(i).getName());} else {goodsName.remove(datas.get(i).getName());}}List<String> tempNames = adapter.removeDuplicate(goodsName);String strNames = StringUtils.listToString(tempNames);tvContent.setText(strNames);}break;}}private void setData(boolean isAll) {for (CleanGoods data : datas) {if (isAll) {data.isCheck = true;adapter.setCheck(data);} else {data.isCheck = false;}}adapter.notifyData(datas);}
}

设置是否全选数据:

private void setData(boolean isAll) {for (CleanGoods data : datas) {if (isAll) {data.isCheck = true;adapter.setCheck(data);} else {data.isCheck = false;}}adapter.notifyData(datas);}

全选:

 setData(true);

全不选:

setData(false);

获取被选中数据的list数组:

if (adapter != null) {for (int i = 0; i < datas.size(); i++) {if (datas.get(i).isCheck) {goodsName.add(datas.get(i).getName());} else {goodsName.remove(datas.get(i).getName());}}List<String> tempNames = adapter.removeDuplicate(goodsName);Log.i("listData",GsonUtils.toJson(tempNames));
}

将list数组转换成string字符串:

String strNames = StringUtils.listToString(tempNames);
tvContent.setText(strNames);

四、如有疑问请留言

Android在recycleview中进行全选和取消全选相关推荐

  1. 在项目中学习.NET的JQuery CheckBox方法(全选、取消全选、其他)

    一.在项目中遇到的CheckBox的全选和取消全选以及其他等解决方案如下: // 对全选和取消全选的事件 $("#CheckAll").click(function () {    ...

  2. axure实现复选框全选_AxureRP8实战手册-案例73(全选与取消全选效果)

    案例73. 全选与取消全选效果 案例来源: 百度音乐-音乐盒 案例效果: 初始状态/取消全选时:(图5-117) 全选后取消任一选项时:(图5-118) 全选/单选全部选中时:(图5-119) 案例描 ...

  3. vue全选和取消全选(无bug)

    很简单的使用vue实现全选和取消全选 直接上代码,简单易懂不懂得可以留言. <!DOCTYPE html> <html lang="en"> <hea ...

  4. AVL_全选_取消全选_打印_ZMM1035

    *&---------------------------------------------------------------------* *& Report ZMM1035 * ...

  5. axure实现复选框全选_Axure RP实例教程:全选与取消全选效果

    原标题:Axure RP实例教程:全选与取消全选效果 Axure RP 9 Mac这款原型设计软件能让设计者快速创建应用软件,或者在web网站的线框图.流程图.原型和规格的设计制作,从低到高的视觉和交 ...

  6. react实现全选、取消全选和个别选择

    react里面实现全选和取消全选,个别选择等操作,效果如下 代码: import React, {Component} from 'react' export default class Demo e ...

  7. DataGridView添加一行数据、全选、取消全选、清空数据、删除选中行

    .net 2005下的Windows Form Application,一个DataGridView控件和4个Button,界面设置如下: 代码如下,有注解,相信大家都看得明白: using Syst ...

  8. php 复选框全选和取消,基于JavaScript实现复选框的全选和取消全选

    这篇文章主要为大家详细介绍了基于JavaScript实现复选框的全选和取消全选,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了js复选框的全选和取消全选的具体代码,供大家参考, ...

  9. 实现全选和取消全选功能

    <body> <div class="wrap"> <table> <thead> <tr> <th> &l ...

  10. 通过js控制layui选择框checkbox的选中、取消选中,以及使用layui实现全选、取消全选的一种方式

    js控制选中.取消选中 ,layui实现全选.取消全选 layui版本2.5.x html部分: <form class="layui-form" id="form ...

最新文章

  1. 通过创建 HttpCookie 对象的实例编写 Cookie
  2. BigData-‘基于代价优化’究竟是怎么一回事?
  3. PIL图像处理:旋转图像
  4. python turtle应用实例_python-turtle-一个简单实例子
  5. Keepalived的LVS配置
  6. 深入浅出面向对象和原型【番外篇——重新认识new】
  7. 精通Java设计模式从初见到相爱之工厂+策略模式(3)
  8. 如果我是博客园的产品经理【上】
  9. oracle删除orcl库_oracle删除数据文件
  10. 值不值得入手_比3系更运动!标配2.0T+后驱,凯迪拉克CT5值不值得入手
  11. SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled
  12. GIT命令行的一些基本操作
  13. 利用朴素贝叶斯算法识别垃圾邮件
  14. Matlab调用excel数据绘制折线图
  15. 计算机统计字符数,如何在电脑上统计文字字数及标点个数
  16. 计算机电源故障引起火灾,计算机硬件的常见故障及维护方法
  17. 已知树节点获取树的节点路径(js树节点路径)
  18. U盘中毒后,文件夹被隐藏的解决方法
  19. Outlook设置新folder location后无法显示default view的解决方案
  20. 【Redis】五种存储类型及其底层数据结构

热门文章

  1. excel做ns流程图_NS流程图是什么图?用这款软件轻松画NS流程图
  2. 王道书 P150 T18(在中序线索二叉树里找指定节点在后序的前驱节点)+ 拓展(在中序线索二叉树里找指定节点在先序的后继节点)
  3. 使用POI在Excel单元格插入符号(Symbol)
  4. excel白屏未响应_EXCEL2016经常卡死,白屏,经常使用过程中卡住,然后显示office正在尝试恢复...
  5. 一个微信群机器人PHP,vbot微信机器人操作联系人的API(3)微信群API
  6. [linux shell] hostid使用方法以及原理
  7. 迅雷 linux 命令行 版本号,linux下完美运行迅雷5.8.9.662
  8. AndroidStudio制作登录和注册功能的实现,界面的布局介绍
  9. Python学习(列表)
  10. 物联网终端安全系列(之二) -- 物联网终端安全需求分析