jeudi 19 janvier 2023

Recyclerview item selection with checkbox, creating a new list/recyclerview with checked items in another fragment

I have a RecyclerView showing movie categories fetched with retrofit from an api. Based on it's preferences the user should be able to show (checkbox is checked) or hide (checkbox is unchecked) categories.

Those setting should proceed in Fragment A. After clicking on a save button (or something similar) the check-state of the items/categries should be saved (for next app starts) and the checked items should be passed in a new list that it's showed in a "new" Recyclerview in Fragment B. I read about Recyclerview-Selection, but it's hard to find a clear step by step tutorial, that's following a similar process as I need (and most of them are written in Java, as I am a completely programming beginner it's even harder to understand it).

So I hope that someone here can help me to get on the right way, be able to implement this also for tv-rv and series-rv.

This is my current adapter, nothing special (in my item-layout there is also a checkbox, id = rvCheckBox) but I am not sure on how to implent it here.

class MovieCategoryAdapter : ListAdapter<Data, MovieCategoryAdapter.ViewHolder>(
    MOVIE_CATEGORY_COMPERATOR) {

    inner class ViewHolder(val binding: RvItemMoviecategoryBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(category: Data) {
            binding.apply {
                rvItemMoviecategory.text = category.title
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            RvItemMoviecategoryBinding.inflate(
                LayoutInflater.from(
                    parent.context
                ),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val moviegenre = getItem(position)!!
        holder.bind(moviegenre)
        holder.binding
    }
    
    companion object {
        private val MOVIE_CATEGORY_COMPERATOR = object : DiffUtil.ItemCallback<Data>() {
            override fun areItemsTheSame(oldItem: Data, newItem: Data) =
                oldItem.id == newItem.id


            override fun areContentsTheSame(oldItem: Data, newItem: Data) =
                oldItem == newItem
        }
    }

}



Aucun commentaire:

Enregistrer un commentaire