dody.ac
dody.ac

Reputation: 77

Materialcardview toggle at Recyclerview Binding not work correctly

I have a problem when toggling cardview at recyclerview, when I'm using kotlin synthetic it's work perfectly. But when I'm migrating to viewBinding, my cardview only toggle last item whenever i clicked any item. can you help me guys?

sorry for bad english

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val list = list[position]
    binding.title.text = list.kategori
    binding.categoryCard.setOnClickListener {
        binding.categoryCard.toggle()
        if (list.isChecked) {
            list.isChecked = false
            if (StepPartner.category.contains(list.id)) StepPartner.category.remove(list.id)
        } else {
            list.isChecked = true
            StepPartner.category.add(list.id)
        }
    }
}

Upvotes: 0

Views: 99

Answers (1)

Sukhvir
Sukhvir

Reputation: 66

you have to use holder to get instance child views, please find below solution:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val list = list[position]
    holder.title.text = list.kategori
    holder.categoryCard.setOnClickListener {
        holder.categoryCard.toggle()
        if (list.isChecked) {
            list.isChecked = false
            if (StepPartner.category.contains(list.id)) StepPartner.category.remove(list.id)
        } else {
            list.isChecked = true
            StepPartner.category.add(list.id)
        }
    }
}

Upvotes: 1

Related Questions