android - RecyclerView取消选择之前选择的复选框

标签 android kotlin android-recyclerview

我将 RecyclerView ViewHolder 作为复选框,单击该复选框后,我需要取消选择已选中的其他复选框,但它没有发生,更改不会反射(reflect)在 View 中。

回收器项目单击在单击时在 onBindViewHolder 中初始化,插槽数据已更新并调用了 notificationItemChanged,但占位符未更新,我是否错过了更新 View 持有者的任何内容。

override fun onBindViewHolder(
    holder: BookingSlotListItemViewHolder,
    position: Int,
    payloads: MutableList<Any?>
 )

class BookingSlotListAdapter(
    private val hourSlotHolder: RecyclerView,
    private val bookingBtn: TextView
    ) : RecyclerView.Adapter<BookingSlotListItemViewHolder>() {
    private val bookingSlots = ArrayList<BookingSlotAvailability>()
    private var lastSelected: Int? = null
    private val bookingHourItemAdapter = BookingHourItemAdapter(bookingBtn)
    private val NOT_SELECTED_FOR_BOOKING = 0
    private val AVAILABLE_FOR_BOOKING = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookingSlotListItemViewHolder {
        val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
        val view = layoutInflater.inflate(R.layout.booking_time_slot, parent, false)
        return BookingSlotListItemViewHolder(view)
    }

    override fun getItemCount(): Int {
        if (bookingSlots.isNullOrEmpty()) {
        return 0
        }
        return bookingSlots.size
    }

    fun setBookingSlots(bookingSlots: TreeMap<String, Boolean>) {
        bookingSlots.map {
            this.bookingSlots.add(BookingSlotAvailability(it.key, it.value, NOT_SELECTED_FOR_BOOKING))
        }
    }

    override fun onBindViewHolder(holder: BookingSlotListItemViewHolder, position: Int) {
        holder.timeSlot.text = bookingSlots[position].time
        if (!bookingSlots[position].isAvailable) {
        holder.timeSlot.isEnabled = false
        }
        if (bookingSlots[position].selectionStatus == 0 && holder.timeSlot.isChecked) {
        holder.timeSlot.toggle()
        } else if (bookingSlots[position].selectionStatus == 1 && !holder.timeSlot.isChecked) {
        holder.timeSlot.toggle()
        }

        holder.timeSlot.setOnClickListener { view: View? ->
        Log.v("CLICKED", "BookingSlotListAdapter: $lastSelected : $position")
        val selectedSlot = view as CheckBox

        if (lastSelected != null) {
            bookingSlots[lastSelected!!].isAvailable = AVAILABLE_FOR_BOOKING
            bookingSlots[lastSelected!!].selectionStatus = NOT_SELECTED_FOR_BOOKING
            notifyItemChanged(
            lastSelected,
            bookingSlots[lastSelected!!]
            )
        }
        bookingSlots[position].isAvailable = NOT_AVAILABLE
        bookingSlots[position].selectionStatus = SELECTED_FOR_BOOKING
        notifyItemChanged(
            position,
            bookingSlots[position]
        )
        lastSelected = position
        }
    }

    override fun onBindViewHolder(
        holder: BookingSlotListItemViewHolder,
        position: Int,
        payloads: MutableList<Any?>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        Log.v("OVERLOADEDVH", "$payloads $position")
    }
}

最佳答案

您需要在 onBindViewholder 方法中对其进行选中/取消选中,因为它会重用 View 并在我们滚动 recyclerView 时再次创建 View ,因此它不知道 checkBox 的最后状态:

    // Add these lines below at then end of onBindViewHolder
    val selectedSlot = holder.timeSlot as CheckBox

    if(lastSelected == position){
        selectedSlot.isChecked = true;
    }else{
        selectedSlot.isChecked = false;
    } 

要更新旧复选框以取消选择,请调用 notifyDataSetChanged(),这样将为所有可见单元格再次调用 onBindHolder(),并相应更新。

在设置 lastSelected pos 后调用 notifyDataSetChanged() :

    holder.timeSlot.setOnClickListener { view: View? ->
    Log.v("CLICKED", "BookingSlotListAdapter: $lastSelected : $position")
    val selectedSlot = view as CheckBox

    if (lastSelected != null) {
        bookingSlots[lastSelected!!].isAvailable = AVAILABLE_FOR_BOOKING
        bookingSlots[lastSelected!!].selectionStatus = NOT_SELECTED_FOR_BOOKING

        //COMMENT  notifyDataSetChanged WILL HANDLE ALL
        //notifyItemChanged(
        //lastSelected,
        //bookingSlots[lastSelected!!]
        //)
    }
    bookingSlots[position].isAvailable = NOT_AVAILABLE
    bookingSlots[position].selectionStatus = SELECTED_FOR_BOOKING

    //COMMENT  notifyDataSetChanged WILL HANDLE ALL
    //notifyItemChanged(
    //    position,
    //    bookingSlots[position]
    //)
    lastSelected = position

     //------CALL notifyDataSetChanged() for all visible cells------
     notifyDataSetChanged() ;


    }

关于android - RecyclerView取消选择之前选择的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57098873/

相关文章:

android - 如何解析本地 JSON 并填充 RecyclerView?

android - 如何从 RecyclerView 中的 EditText 获取字符串值?

android多线程程序速度慢

android - 使用折叠工具栏滚动到回收 View 的最后一项

java - RecyclerView,notifyItemRemoved有时会删除无效项目,notifyDataSetChanged工作完美

kotlin - java.lang.NoClassDefFoundError : com/fasterxml/jackson/databind/JsonDeserializer 错误

android - Kotlin 协程 `runBlocking`

android - 当图像不适合屏幕时缩小图像

android - RecyclerView - 在 notifiyItemInserted(0) 之后保持在 0 位置

java - Fragment 内 RecyclerView 内 OnItemClickListener 的奇怪行为