简体   繁体   中英

checked checkbox become unchecked in recyclerview after scrolling

I have a recyclerview with a list of items , when i check an item and scroll until the checked checkbox disppear , and scroll again to it , the checkbox become unchecked

this is my adapter code

class ContactsAdapter(var list:ArrayList<contact>, private val listener: (contact) -> Unit):RecyclerView.Adapter<ContactsAdapter.viewHolder>() {

    inner class viewHolder(view:View):RecyclerView.ViewHolder(view)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewHolder {
        var view = LayoutInflater.from(parent.context).inflate(R.layout.item_ui, parent, false)
        return viewHolder(view)
    }
    override fun onBindViewHolder(holder: viewHolder, position: Int) {
        var itemPosition = list[position]
        holder.itemView.apply {
            contact_name.text=itemPosition.contactName
            contact_number.text=itemPosition.contactNumber[0]
            check_number.setOnClickListener {
                listener(itemPosition)
            }
            check_number.isChecked = itemPosition.checked
}
    }
    override fun getItemCount(): Int {
       return list.size
    }
}

A RecyclerView recycles its rows. Your setOnClickListener() needs to do something to hold onto which items are checked and unchecked, and your onBindViewHolder() needs to update the CheckBox when it binds an item. It seems like the second part is implemented, but perhaps not the first part. You may need to update itemPosition.checked in your lambda for setOnClickListener() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM