RecyclerView

RecycleView CheckBox全選、單選 資料不錯亂

1.創建Adapter

class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    interface GetDataCallBack {
        fun getDataChange(data: ArrayList<Int>)
    }

    fun setDataCallBack(getDataCallBack: GetDataCallBack) {
        this.getDataCallBack = getDataCallBack
    }

    private var data = arrayListOf<Data>()
    private val choices = hashMapOf<Int, Boolean>()
    lateinit var getDataCallBack: GetDataCallBack


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        )

    override fun getItemCount() = data.size

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemImage.setImageResource(data[position].image)
        holder.itemCheck.isChecked = choices.getValue(position)
        holder.itemCheck.setOnClickListener {
            choices[position] = !choices[position]!!
            notifyDataSetChanged()
        }
    }

    //塞資料
    fun setData(data: ArrayList<Data>) {
        this.data = data
        data.forEachIndexed { index, _ ->
            choices[index] = false
        }
        notifyDataSetChanged()
    }

    //讀取勾選資料
    fun getData() {
        val data = arrayListOf<Int>()
        choices.forEach {
            if (it.value) {
                data.add(it.key)
            }
        }
        data.sort()
        getDataCallBack.getDataChange(data)
    }

    //反轉
    fun reverseAll() {
        choices.entries.forEach {
            it.setValue(!it.value)
        }
        notifyDataSetChanged()
    }

    //全選
    fun getAll() {
        var shouldAll = false
        choices.entries.forEach {
            val value = it.value
            if (!value) {
                shouldAll = true
                return@forEach
            }
        }
        choices.entries.forEach {
            it.setValue(shouldAll)
        }
        notifyDataSetChanged()
    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var itemImage: ImageView = itemView.findViewById(R.id.image)
        var itemCheck: CheckBox = itemView.findViewById(R.id.checkBox)
    }
}

2.使用Adapter方法

class MainActivity : AppCompatActivity() {

    lateinit var myAdapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView(binding)
        val data = initData()

        myAdapter.setDataCallBack(object : MyAdapter.GetDataCallBack {
            override fun getDataChange(data: ArrayList<Int>) {
                Toast.makeText(this@MainActivity, data.toString(), Toast.LENGTH_SHORT).show()
            }
        })

        myAdapter.setData(data)
    }

    private fun initData(): ArrayList<Data> {
        val data = arrayListOf<Data>()
        for (i in 0..15) {
            data.add(Data(R.drawable.girl))
        }
        return data
    }

    private fun initView(binding: ActivityMainBinding) {
        binding.recyclerview.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            setHasFixedSize(true)
            myAdapter = MyAdapter()
            addItemDecoration(
                DividerItemDecoration(
                    this@MainActivity,
                    DividerItemDecoration.VERTICAL
                )
            )
            adapter = myAdapter
        }
    }

    fun all(view: View) {
        myAdapter.getAll()
    }

    fun get(view: View) {
        myAdapter.getData()
    }

    fun reverseAll(view: View) {
        myAdapter.reverseAll()
    }
}

3.效果展示

發表迴響