Как по нажатию выбрать все checkbox Android?

У меня в RecyclerView, в фрагменте, есть несколько итемов с checkbox, и один checkbox в самом фрагменте. По выбору checkbox'в, в RecyclerView, меняю в листе значение checked на true и по нажатию на кнопку в фрагменте? удаляю нужные мне. По отдельности всё работает правильно но когда нажимаю на общий checkbox в фрагменте, удаление работает правильно а checkbox'ы в RecyclerView не меняются. Вот мой код:

class RemovingAppsFragment : Fragment() {
    private var _binding: FragmentRemovingAppsBinding? = null
    private val binding get() = _binding!!
    private lateinit var appsArrayList: ArrayList<AppInfo>
    private var isAllAppsChoosen = false

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentRemovingAppsBinding.inflate(inflater, container, false)
        val root: View = binding.root
        setVideoSphere()

        appsArrayList = Utils.getAppsListInfo(requireActivity())

        initRecyclerView()

        binding.removeAppsAllCheckBox.setOnClickListener {
            if (isAllAppsChoosen) {
                for (app in appsArrayList) {
                    app.checked = false
                }
                isAllAppsChoosen = false
            } else {
                for (app in appsArrayList) {
                    app.checked = true
                }
                isAllAppsChoosen = true
            }
        }

        return root
    }

    private fun initRecyclerView() {
        binding.removeAppsRecyclerView.setHasFixedSize(true)
        val adapter = AppsAdapterList(requireActivity(), appsArrayList)
        binding.removeAppsRecyclerView.layoutManager = LinearLayoutManager(requireActivity().applicationContext, LinearLayoutManager.VERTICAL, false)
        binding.removeAppsRecyclerView.adapter = adapter
    }

Adapter

class AppsAdapterList(private val activity: Activity? = null, private var recyclerList: ArrayList<AppInfo>? = null) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        return (holder as ViewHolder).bind(recyclerList!![position])
    }

    override fun getItemCount(): Int {
        return recyclerList!!.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ViewHolder(LayoutInflater.from(activity).inflate(R.layout.apps_with_ckeckbox_card_item, parent, false))
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private var binding: AppsWithCkeckboxCardItemBinding = AppsWithCkeckboxCardItemBinding.bind(view)

        @SuppressLint("NotifyDataSetChanged")
        fun bind(apps: AppInfo) {
            binding.apply {
                appsIcon.setImageDrawable(apps.appIcon)
                appsName.text = apps.appName
                appsCheckBox.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
                    recyclerList!![position].checked = isChecked
                    notifyDataSetChanged()
                }

            }

        }
    }

}

Использую AppCompatCheckBox

Подскажите пожалуйста, что нужно поправить, чтобы всё работало правильно?! Варианты от сюда, https://stackoverflow.com/questions/39329079/select-all-checkboxes-in-recyclerview пробовал, не работает.


Ответы (0 шт):