美文网首页
使用RecycleView实现单选,多选,全选。

使用RecycleView实现单选,多选,全选。

作者: SeekLife0 | 来源:发表于2022-01-03 10:54 被阅读0次

    1、应用场景。
    如:购物车的删除功能。
    概述:正常情况为普通浏览状态,点击右上角设置进入删除状态,处于删除状态则会显示删除,全选等按钮,再点击设置返回浏览状态则隐藏删除,全选按钮,且每次切换的时候都会同时消除当前item项的选中状态。

    正常情况.png
    删除状态.png

    2、使用组件
    BaseQuickAdapter
    RecycleView
    CheckBox
    Kongzue.baseframework
    TitleBar -> github : https://github.com/getActivity/TitleBar

    3、实现过程
    思路:代码虽然多,但思路很简单,在Adapter中使用map存储列表项的id和选中状态,然后每次通过notifyDataSetChanged()方法来修改RecyclerView的显示。
    我这边项目的应用场景由于限制了列表项的数量,只展示了一页内容,所以全选的时候会全部选中,如果说内容超过1页,那么点击全选的时候只会选择第1页的内容,第二页内容则不会选中,原因是我设置adapter在第一次加载数据的时候把当前RecyclerView的item装填到map这个id,状态集合中,全选实际上只是改变这个集合的状态。所以后续加载的数据没有被装填进去自然选中状态无法被改变。由于每次删除会重新刷新数据,recyclerView又会被视为第一次加载数据,数据被装填到map中,所以后续的操作都正常,只有加载更多数据的时候选择状态会中断。解决办法,限制该页面item的显示个数。
    1、布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".packaged_products.PackagedContrastManagerActivity">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/white"
            >
    
            <com.hjq.shape.layout.ShapeFrameLayout
                android:layout_width="match_parent"
                android:layout_height="140dp"
                app:shape_bottomLeftRadius="10dp"
                app:shape_bottomRightRadius="10dp"
                app:shape_solidColor="#00C99D">
    
                <com.hjq.bar.TitleBar
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#00C99D"
                    app:barStyle="transparent"
                    app:lineVisible="false"
                    app:rightIcon="@mipmap/setup_white"
                    app:rightIconHeight="25dp"
                    app:rightIconWidth="25dp"
                    app:title="套餐对比"
                    app:titleColor="@color/white" />
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="8dp"
                    android:src="@mipmap/city_illustration" />
            </com.hjq.shape.layout.ShapeFrameLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="570dp"
                android:orientation="vertical"
                android:layout_marginTop="100dp"
                >
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/rc_contrast"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:paddingHorizontal="7dp"
                        android:overScrollMode="never"
                        />
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/ll_start_contrast"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:gravity="center"
                android:orientation="horizontal"
                android:visibility="visible">
    
                <com.hjq.shape.view.ShapeTextView
                    android:id="@+id/stv_start_contrast"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingVertical="7dp"
                    android:text="开始对比"
                    android:textColor="@color/white"
                    app:shape_radius="10dp"
                    app:shape_solidColor="#00C99D" />
            </LinearLayout>
    
            <RelativeLayout
                android:id="@+id/rl_select_all_delete"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal"
                android:visibility="gone">
    
                <CheckBox
                    android:id="@+id/cb_select_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="50dp"
                    android:button="@drawable/bg_contrast_item_select_middle"
                    android:paddingHorizontal="10dp"
                    android:text="全选" />
    
                <com.hjq.shape.view.ShapeTextView
                    android:id="@+id/stv_contrast_delete"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="50dp"
                    android:gravity="center"
                    android:paddingVertical="7dp"
                    android:text="删除"
                    android:textColor="@color/white"
                    app:shape_radius="10dp"
                    app:shape_solidColor="#00C99D" />
            </RelativeLayout>
    
        </RelativeLayout>
    
    </LinearLayout>
    

    2、Activity

    package com.example.xy.packaged_products
    
    import android.graphics.Color
    import android.util.Log
    import android.view.View
    import android.widget.CheckBox
    import android.widget.LinearLayout
    import android.widget.RelativeLayout
    import android.widget.TextView
    import androidx.recyclerview.widget.LinearLayoutManager
    import androidx.recyclerview.widget.RecyclerView
    import com.blankj.utilcode.util.ToastUtils
    import com.example.xy.R
    import com.example.xy.address.adapter.ContrastManagerAdapter
    import com.example.xy.entity.ContrastDeleteData
    import com.example.xy.entity.ContrastList
    import com.example.xy.utils.AppSharedPreferences
    import com.example.xy.utils.Constant
    import com.example.xy.utils.StatusBarUtils
    import com.hjq.bar.OnTitleBarListener
    import com.hjq.bar.TitleBar
    import com.hjq.shape.view.ShapeCheckBox
    import com.hjq.shape.view.ShapeTextView
    import com.kongzue.baseframework.BaseActivity
    import com.kongzue.baseframework.interfaces.BindView
    import com.kongzue.baseframework.interfaces.DarkStatusBarTheme
    import com.kongzue.baseframework.interfaces.Layout
    import com.kongzue.baseframework.util.JumpParameter
    import com.kongzue.dialogx.dialogs.CustomDialog
    import com.kongzue.dialogx.interfaces.OnBindView
    import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
    import rxhttp.RxHttp
    
    @Layout(R.layout.activity_packaged_contrast_manager)
    @DarkStatusBarTheme(true)
    class PackagedContrastManagerActivity : BaseActivity() {
    
        @BindView(R.id.rc_contrast)
        private lateinit var rcContrast: RecyclerView
    
        @BindView(R.id.rl_select_all_delete)
        private lateinit var rlAllDelete: RelativeLayout
    
        @BindView(R.id.ll_start_contrast)
        private lateinit var startContrast: LinearLayout
    
        @BindView(R.id.stv_contrast_delete)
        private lateinit var deleteBtn: ShapeTextView
    
        @BindView(R.id.cb_select_all)
        private lateinit var selectedAll: CheckBox
    
        @BindView(R.id.title)
        private lateinit var titleBar : TitleBar
    
        //当前显示的所有数据
        private lateinit var listData: MutableList<ContrastList.Data>
        private val contrastManagerAdapter = ContrastManagerAdapter()
        private var currentPage = 1
        private var changeButton = 1 //0是删除 1是对比
    
        //选中的套餐加入对比id集合
        private var selectedContrastIds: MutableList<Int> = mutableListOf()
    
        //选中的套餐加入删除id集合
        private var selectedDeleteIds: MutableList<Int> = mutableListOf()
    
        override fun initViews() {
            StatusBarUtils.setLightStatusBar(this,false) //设置状态栏文字颜色为白颜色
            titleBar.setOnTitleBarListener(object :
                OnTitleBarListener {
                override fun onLeftClick(view: View?) {
                    finish()
                }
                override fun onTitleClick(view: View?) {//显示删除和全选按钮
                }
                override fun onRightClick(view: View?) {
                    if (changeButton == 0) {  //变为加入模式
                        //显示添加操作
                        rlAllDelete.visibility = RelativeLayout.GONE
                        startContrast.visibility = LinearLayout.VISIBLE
                        //从删除返回到加入模式时恢复未选择状态
                        contrastManagerAdapter.echoDeleteChange(false)
                        contrastManagerAdapter.setDeleteChange(
                            false,
                            selectedDeleteIds,
                            true,
                            1
                        ) //flag=0此时不会走加入模式
                        changeButton = 1
                        //删除模式变为加入模式,加入模式的选择状态进行清空
                        contrastManagerAdapter.setClearAdd(true)
                    } else {  //变为删除模式
                        rlAllDelete.visibility = RelativeLayout.VISIBLE
                        //隐藏开始对比
                        startContrast.visibility = LinearLayout.GONE
                        //从加入到删除时恢复为未选择状态
                        contrastManagerAdapter.echoDeleteChange(false)
                        contrastManagerAdapter.setDeleteChange(
                            false,
                            selectedDeleteIds,
                            true,
                            1
                        )  //最后一个参数,当有新的item加入进来时更新adapter的list
                        changeButton = 0
                        //删除模式变为加入模式,加入模式的选择状态进行清空
                        contrastManagerAdapter.setClearAdd(true)
                    }
                }
            })
            RequestListData(currentPage)
        }
    
        override fun initDatas(parameter: JumpParameter?) {
        }
    
        override fun setEvents() {
            //跳转到对比界面
            val stvStartContrast = findViewById<ShapeTextView>(R.id.stv_start_contrast)
            stvStartContrast.setOnClickListener {
                if (selectedContrastIds.size == 2) {
                    //跳转时传入对比的id
                    var jp = JumpParameter()
                    jp.put("firstWholeId", selectedContrastIds[0])
                    jp.put("secondWholeId", selectedContrastIds[1])
                    jump(ProductsContrastActivity::class.java, jp)
                } else {
                    ToastUtils.showShort("请选择两个套餐")
                }
            }
    
            //点击删除,删除列表中对应套餐
            deleteBtn.setOnClickListener(View.OnClickListener {
                if (selectedDeleteIds != null && selectedDeleteIds.size == 0) {
                    //弹出提示没有任何东西被选中
                    ToastUtils.showShort("没有任何套餐被选中")
                } else {
                    //进行弹窗提示
                    CustomDialog.build().setMaskColor(Color.parseColor("#4D000000"))
                        .setCustomView(object :
                            OnBindView<CustomDialog>(R.layout.dialog_address_delete) {
                            override fun onBind(dialog: CustomDialog?, v: View?) {
                                val dialogText = v?.findViewById<TextView>(R.id.tv_content)
                                val stvCall = v?.findViewById<ShapeTextView>(R.id.stv_delete)
                                val stvBtn = v?.findViewById<ShapeTextView>(R.id.stv_cancel)
                                stvCall?.text = "删除"
                                stvCall?.setTextColor(Color.parseColor("#FF3030"))
                                dialogText?.text = "是否删除当前对比套餐"
                                stvBtn?.setOnClickListener {
                                    dialog?.dismiss()
                                }
                                stvCall?.setOnClickListener {
                                    dialog?.dismiss()
                                    RequestDeleteData(selectedDeleteIds)
                                    //提交请求之后需要把selectedDeleteIds清除
                                    contrastManagerAdapter.echoDeleteChange(true)
                                    //点击删除之后需要把adapter的item状态集合一并删除
                                    contrastManagerAdapter.setDeleteChange(
                                        false,
                                        selectedDeleteIds,
                                        false,
                                        0
                                    )
                                }
                            }
                        }).setAlign(CustomDialog.ALIGN.CENTER).show()
                }
            })
    
            //删除-->点击全选,选择所有套餐对比数据
            selectedAll.setOnCheckedChangeListener { buttonView, isChecked ->
                //通过adapter来改变选择状态
                if (isChecked) {
                    //添加所有
                    contrastManagerAdapter.echoDeleteChange(false)
                    contrastManagerAdapter.setDeleteChange(true, selectedDeleteIds, true, 0)
                } else {
                    //移除所有
                    contrastManagerAdapter.echoDeleteChange(false)
                    contrastManagerAdapter.setDeleteChange(false, selectedDeleteIds, true, 0)
                }
            }
        }
    
        //绑定数据到recyclerView
        private fun bindData() {
            rcContrast.layoutManager = LinearLayoutManager(me)
            rcContrast.adapter = contrastManagerAdapter
            //设置没有数据时的视图
            contrastManagerAdapter.setEmptyView(R.layout.nothing_selected_space)
            //设置模块点击事件
            contrastManagerAdapter.setOnItemClickListener { adapter, view, position ->
            }
            //设置模块子view的点击事件
            contrastManagerAdapter.addChildClickViewIds(R.id.rcb_select)
    
            contrastManagerAdapter.setOnItemChildClickListener { adapter, view, position ->
                Log.e("套餐对比加入", "单选按钮是否被点击")
                var contrastItem = adapter.getItem(position) as ContrastList.Data
                //判断当前是删除还是加入对比
                if (changeButton == 0) {  //删除
                    //删除
                    val stvBtn = view.findViewById<ShapeCheckBox>(R.id.rcb_select)
                    if (stvBtn.isChecked) {
                        //选中添加
                        Log.e(
                            "套餐对比加入",
                            "添加到删除list中 wholeId-->" + contrastItem.wholeId + "   状态---> " + stvBtn.isChecked
                        )
                        selectedDeleteIds.add(contrastItem.contrastId)
                        //当状态被改变时,根据contrastItem.contrastId的位置改变对应的状态
                        contrastManagerAdapter.echoDeleteChange(false)
                        contrastManagerAdapter.setDeleteItemTag(
                            contrastItem.contrastId,
                            stvBtn.isChecked,
                            false
                        )
    
                    } else {
                        //未选中移除
                        Log.e(
                            "套餐对比加入",
                            "从删除list中移除 wholeId-->" + contrastItem.wholeId + "   状态---> " + stvBtn.isChecked
                        )
                        selectedDeleteIds.remove(contrastItem.contrastId)
                        //当状态被改变时,根据contrastItem.contrastId的位置改变对应的状态
                        contrastManagerAdapter.echoDeleteChange(false)
                        contrastManagerAdapter.setDeleteItemTag(
                            contrastItem.contrastId,
                            stvBtn.isChecked,
                            false
                        )
                    }
                } else {   //加入对比
                    val stvBtn = view.findViewById<ShapeCheckBox>(R.id.rcb_select)
                    Log.e("套餐对比加入", "当前选择套餐对比数量" + selectedContrastIds.size)
                    if (stvBtn.isChecked) {
                        if (selectedContrastIds.size >= 2) {
                            //提示只能双选
                            ToastUtils.showShort("套餐对比数量已达上限")
                            //改变按钮状态
                            contrastManagerAdapter.setAddItemTag(
                                contrastItem.wholeId,
                                stvBtn.isChecked,
                                selectedContrastIds
                            )
                        } else {
                            //选中添加
                            Log.e(
                                "套餐对比加入",
                                "添加到加入list中 wholeId-->" + contrastItem.wholeId + "   状态---> " + stvBtn.isChecked
                            )
                            selectedContrastIds.add(contrastItem.wholeId) //contrastId
                            //改变按钮状态
                            contrastManagerAdapter.setAddItemTag(
                                contrastItem.wholeId,
                                stvBtn.isChecked,
                                selectedContrastIds
                            )
                        }
                    } else {
                        //未选中移除
                        Log.e(
                            "套餐对比加入",
                            "从加入list中移除 wholeId-->" + contrastItem.wholeId + "   状态---> " + stvBtn.isChecked
                        )
                        selectedContrastIds.remove(contrastItem.wholeId) //contrastId
                        //改变按钮状态
                        contrastManagerAdapter.setAddItemTag(
                            contrastItem.wholeId,
                            stvBtn.isChecked,
                            selectedContrastIds
                        )
                    }
                }
            }
        }
    
        //获取列表后端数据
        private fun RequestListData(pageNum: Int) {
            //rxhttp获取后台数据
            RxHttp.postJson(Constant.CONTRASTLIST)
                .addHeader(Constant.TOKEN, AppSharedPreferences.getInstance(me).token)
                .add("limit", Constant.PAGESIZE)
                .add("page", pageNum)
                .asClass(ContrastList::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ a ->
                    Log.e("产品对比列表调试", "数据:" + a.data)
                    if (a.data != null) {
                        //将数据绑定到列表中
                        listData = a.data
                        Log.e("产品对比列表", "初始化数据" + listData.size)
                        contrastManagerAdapter.setNewInstance(a.data)
                        bindData()
                        if(contrastManagerAdapter.data.size == 0){
                            //隐藏按钮
                            startContrast.visibility = LinearLayout.GONE
                            rlAllDelete.visibility = RelativeLayout.GONE
                            titleBar.rightView.visibility = View.GONE
                            Log.e("测试空数据时按钮是否隐藏","已隐藏")
                        }
                    }
                }, { throwable ->
                    Log.e("产品对比列表调试", "error:" + throwable)
                })
        }
    
        //删除数据
        private fun RequestDeleteData(contrastIds: MutableList<Int>) {
            //rxhttp获取后台数据
            RxHttp.postJson(Constant.DELETECONTRAST)
                .addHeader(Constant.TOKEN, AppSharedPreferences.getInstance(me).token)
                .add("contrastIds", contrastIds)
                .asClass(ContrastDeleteData::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ a ->
                    Log.e("产品对比列表调试", "是否删除成功:" + a.isSuccess)
                    //静态刷新
                    currentPage = 1
                    RequestListData(currentPage)
                }, { throwable ->
                    Log.e("产品对比列表调试", "error:" + throwable)
                })
        }
    
        //加载更多数据
        private fun RequestMoreData(pageNum: Int) {
            //rxhttp获取后台数据
            RxHttp.postJson(Constant.CONTRASTLIST)
                .addHeader(Constant.TOKEN, AppSharedPreferences.getInstance(me).token)
                .add("limit", Constant.PAGESIZE)
                .add("page", pageNum)
                .asClass(ContrastList::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ a ->
                    Log.e("产品对比列表调试", "success:" + a.data)
                    //将数据绑定到列表中
                    listData = a.data
                    Log.e("产品对比列表", "初始化数据" + listData.size)
                    contrastManagerAdapter.addData(listData)
                }, { throwable ->
                    Log.e("产品对比列表调试", "error:" + throwable)
                })
        }
    
    }
    

    3、Adapter

    package com.example.xy.address.adapter
    
    import android.util.Log
    import android.widget.ImageView
    import com.bumptech.glide.Glide
    import com.chad.library.adapter.base.BaseQuickAdapter
    import com.chad.library.adapter.base.viewholder.BaseViewHolder
    import com.example.xy.R
    import com.example.xy.entity.ContrastList
    import com.hjq.shape.view.ShapeCheckBox
    
    class ContrastManagerAdapter :
        BaseQuickAdapter<ContrastList.Data, BaseViewHolder>(R.layout.item_contrast_manager) {
    
        //0->删除模式 1->开始对比模式
        private var flag: Int = 0
    
        //删除模式--------------------------------------0
        private var selectedDeleteIds: MutableList<Int> = mutableListOf()
    
        //是否开启全选标志
        private var isSelected: Boolean = false
    
        //是否全选/反选
        private var allSelected: Boolean = false
    
        //存储删除集合选中状态
        private var deleteItemTags: MutableMap<Int, Boolean> =
            mutableMapOf()//MutableList<Boolean> = mutableListOf()
    
        //是否是第一次初始化
        public var startTag: Int = 0
    
        //是否已被删除标志
        private var isDel: Boolean = false
    
        //加入对比模式-------------------------------------1
        private var selectedContrastIds: MutableList<Int> = mutableListOf()
    
        private var isClear = false //是否需要清空
    
        //存储加入对比集合选中状态
        private var addItemTags: MutableMap<Int, Boolean> =
            mutableMapOf()
    
        override fun convert(holder: BaseViewHolder, item: ContrastList.Data) {
            //初始化删除item项状态集合
            if (startTag == 0) {
                deleteItemTags.put(
                    item.contrastId,
                    item.flagDelete
                )
                //正常绑定数据
                Glide.with(context).load(item.wholeImage)
                    .into(holder.getView<ImageView>(R.id.stv_selected_space))
                holder.setText(R.id.tv_selected_1, item.title)
            } else {
                //无操作
            }
    
            //初始化加入item项状态集合
            if (startTag == 0) {
                addItemTags[item.wholeId] = item.flagAdd
                //正常绑定数据
                Glide.with(context).load(item.wholeImage)
                    .into(holder.getView<ImageView>(R.id.stv_selected_space))
                holder.setText(R.id.tv_selected_1, item.title)
            } else {
                //无操作
            }
    
            if (flag == 0) {  //删除
                //判断是否全选
                if (isSelected) {
                    deleteAllSelector(holder, item)
                    if(isClear){ //清空选择状态
                        clearAdd(holder, item)
                    }
                } else {
                    deleteMultipleSelector(holder, item)
                }
            } else { //添加
                if (selectedContrastIds.size >= 2) { //双选
                    doubleSelector(holder, item)
                } else { //正常多选
                    addMultipleSelector(holder, item)
                }
            }
        }
    
        //删除模式---------------------------------------------------0
        //删除多选状态改变
        fun setDeleteItemTag(position: Int, isChecked: Boolean, isSelected: Boolean) {
            this.deleteItemTags[position] = isChecked
            this.startTag = 1
            this.flag = 0
            //禁用全选
            this.isSelected = isSelected
            //改变后更新数据
            notifyDataSetChanged()
        }
    
        //删除多选选择器
        fun deleteMultipleSelector(holder: BaseViewHolder, item: ContrastList.Data) {
            Log.e("套餐对比adapter", "position匹配开始修改状态")
            //绑定图片
            Glide.with(context).load(item.wholeImage)
                .into(holder.getView<ImageView>(R.id.stv_selected_space))
            //绑定文字
            holder.setText(R.id.tv_selected_1, item.title)
            //判断当前是否是删除点击事件
            if (isDel) {
                //先判断是否有item被删除了,删除了把状态一并删除
                for (it in selectedDeleteIds) {
                    if (it == item.contrastId) {
                        //deleteItemTags.removeAt(it)
                        deleteItemTags.remove(it)
                        Log.e(
                            "移除选择状态",
                            "选择状态的大小" + "选择状态集合大小-->" + deleteItemTags.entries.size
                        )
                    }
                }
            }
            //新设置为选中----使用map来绑定每一个item项和他的选中状态
            var rcb = holder.getView<ShapeCheckBox>(R.id.rcb_select)
            for (it in deleteItemTags.entries.withIndex()) {  //withIndex())
                if (item.contrastId == it.value.key) {
                    rcb.isChecked = it.value.value
    //                if (it.value.value) {
    //                    //添加到删除集合
    //                    selectedDeleteIds.add(item.contrastId)
    //                } else {
    //                    //移除删除集合
    //                    selectedDeleteIds.remove(item.contrastId)
    //                }
                } else {
    
                }
            }
        }
    
        //响应删除
        fun echoDeleteChange(isDel: Boolean) {
            this.isDel = isDel
        }
    
        //删除模式--全选反选状态改变
        fun setDeleteChange(
            allSelected: Boolean,
            selectedDeleteIds: MutableList<Int>,
            isSelected: Boolean,
            startTag: Int
        ) {
            this.allSelected = allSelected
            this.selectedDeleteIds = selectedDeleteIds
            this.isSelected = isSelected
            this.startTag = startTag
            this.flag = 0
            notifyDataSetChanged()
        }
    
        //全选,反选选择器
        fun deleteAllSelector(holder: BaseViewHolder, item: ContrastList.Data) {
            //当前被选中项需要改为被点击状态,其他组件状态不改变。
            //全选
            if (allSelected) {
                //绑定图片
                Glide.with(context).load(item.wholeImage)
                    .into(holder.getView<ImageView>(R.id.stv_selected_space))
                //绑定文字
                holder.setText(R.id.tv_selected_1, item.title)
                //设置为选中
                var rcb = holder.getView<ShapeCheckBox>(R.id.rcb_select)
                rcb.isChecked = true
                //添加全部到删除集合,过滤已选中
                if(deleteItemTags[item.contrastId]==false){
                    //只有未选中才进行删除请求列表添加
                    selectedDeleteIds.add(item.contrastId)
                }
                //需要把集合内的flag全部设置为true
                deleteItemTags[item.contrastId] = true
            } else { //未全选
                //绑定图片
                Glide.with(context).load(item.wholeImage)
                    .into(holder.getView<ImageView>(R.id.stv_selected_space))
                //绑定文字
                holder.setText(R.id.tv_selected_1, item.title)
                //设置为未选中
                var rcb = holder.getView<ShapeCheckBox>(R.id.rcb_select)
                rcb.isChecked = false
                //移除全部删除集合
                selectedDeleteIds.clear()//remove(item.contrastId)
                //需要把结合内的flag全部设置为false
                deleteItemTags[item.contrastId] = false
            }
        }
    
        //加入对比模式------------------------------------------------------1
    
        //加入多选状态改变
        fun setAddItemTag(position: Int, isChecked: Boolean, selectedContrastIds: MutableList<Int>) {
            this.addItemTags[position] = isChecked
            this.selectedContrastIds = selectedContrastIds
            this.startTag = 1
            this.flag = 1
            this.isClear = false
            //改变后更新数据
            notifyDataSetChanged()
        }
    
        //清空状态
        fun setClearAdd(isClear : Boolean){
            this.isClear = isClear
            //改变后更新数据
            notifyDataSetChanged()
        }
    
        //加入多选选择器
        fun addMultipleSelector(holder: BaseViewHolder, item: ContrastList.Data) {
            //绑定图片
            Glide.with(context).load(item.wholeImage)
                .into(holder.getView<ImageView>(R.id.stv_selected_space))
            //绑定文字
            holder.setText(R.id.tv_selected_1, item.title)
            //新设置为选中----使用map来绑定每一个item项和他的选中状态
            var rcb = holder.getView<ShapeCheckBox>(R.id.rcb_select)
            for (it in addItemTags.entries.withIndex()) {
                if (item.wholeId == it.value.key) {
                    rcb.isChecked = it.value.value
                } else {
    
                }
            }
        }
    
        //加入双选选择器
        fun doubleSelector(holder: BaseViewHolder, item: ContrastList.Data) {
            //绑定图片
            Glide.with(context).load(item.wholeImage)
                .into(holder.getView<ImageView>(R.id.stv_selected_space))
            //绑定文字
            holder.setText(R.id.tv_selected_1, item.title)
            //新设置为选中----使用map来绑定每一个item项和他的选中状态
            var rcb = holder.getView<ShapeCheckBox>(R.id.rcb_select)
            if (selectedContrastIds.contains(item.wholeId)) {
                for (i in addItemTags.entries.withIndex()) {  //withIndex())
                    if (item.wholeId == i.value.key) {
                        rcb.isChecked = i.value.value
                    } else {
    
                    }
                }
            } else {
                rcb.isChecked = false
                addItemTags[item.wholeId] = false
            }
        }
    
        //加入清空选择状态(切换删除加入时使用)
        fun clearAdd(holder: BaseViewHolder, item: ContrastList.Data){
            //加入列表全部变为未选状态
            //绑定图片
            Glide.with(context).load(item.wholeImage)
                .into(holder.getView<ImageView>(R.id.stv_selected_space))
            //绑定文字
            holder.setText(R.id.tv_selected_1, item.title)
            var rcb = holder.getView<ShapeCheckBox>(R.id.rcb_select)
    //        rcb.isChecked = false //设置为未选状态
            selectedContrastIds.clear() //清空加入对比数据
            //清空所有加入对比选择状态,即把加入列表选择状态全部置为false
            for(item in addItemTags){
                item.setValue(false)
            }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:使用RecycleView实现单选,多选,全选。

          本文链接:https://www.haomeiwen.com/subject/rnrxcrtx.html