我将RecyclerView ViewHolder用作复选框,单击该复选框后,我需要取消选择已经选择的另一个复选框,但是这没有发生,更改未反映在视图中。
单击时在onBindViewHolder中初始化Recycler项目单击,更新插槽数据并调用notifyItemChanged,但不更新占位符,我在更新视域时是否会错过任何内容。
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")
}
}
答案 0 :(得分:2)
您需要在onBindViewholder方法中将其选中/取消选中,因为当我们滚动recyclerView时,它会重用视图并再次创建视图,因此它不知道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()
,并进行相应的更新。
在设置notifyDataSetChanged()
pos后致电lastSelected
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() ;
}