6

这是我的 RecyclerView 适配器类。单击列表 UI 中的项目应将特定项目(如背景黑色变为白色)更改。单击一个项目后,我将 UI 类中项目 0 的状态更改为 1,基于该状态 UI 应该更改项目。但在 diffUtil areContentTheSame() 内部返回 true。

class TimeSlotsAdapter(val adapterItemOnClick: (TimeSlots) -> Unit):RecyclerView.Adapter<TimeSlotsAdapter.TimeSlotsViewHolder>() {

private lateinit var binding: RvTimeSlotsBinding
private var timeSlots: List<TimeSlots> = ArrayList<TimeSlots>()


inner class TimeSlotsViewHolder : RecyclerView.ViewHolder {
    constructor(rvBinding: RvTimeSlotsBinding) : super(rvBinding.root) {
    }

    fun setItem(timeSlots: TimeSlots) {
        binding.timeSlotView.setOnClickListener { adapterItemOnClick(timeSlots) }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimeSlotsViewHolder {
    binding =
        RvTimeSlotsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return TimeSlotsViewHolder(binding)
}

override fun onBindViewHolder(holder: TimeSlotsViewHolder, position: Int) {
    timeSlots[position]?.let { currentSlot ->
        with(holder) {

            if (currentSlot.status == "1") {
                binding.timeSlotView.background =
                    ContextCompat.getDrawable(
                        binding.timeSlotView.context,
                        R.drawable.time_slot_item_bg
                    )
                binding.timeSlotTv.setTextColor(
                    ContextCompat
                        .getColor(binding.timeSlotTv.context, R.color.black)
                )
                binding.timeSlotTv.text = "1 " + currentSlot.time

                Timber.e("${currentSlot.time} ${currentSlot.status}")
            } else {
                binding.timeSlotView.background =
                    ContextCompat.getDrawable(
                        binding.timeSlotView.context,
                        R.drawable.black_bg
                    )
                binding.timeSlotTv.setTextColor(
                    ContextCompat
                        .getColor(binding.timeSlotTv.context, R.color.white)
                )
                binding.timeSlotTv.text = "0 " + currentSlot.time
                Timber.e("${currentSlot.time} ${currentSlot.status}")
            }
            setItem(currentSlot)
        }
    }
}

override fun getItemCount(): Int = timeSlots.size

fun submitSlots(timeSlotList: List<TimeSlots>) {
    val oldList = timeSlots
    val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(
        TimeSlotsDiffCallBack(
            oldList,
            timeSlotList
        )
    )
    timeSlots = timeSlotList
    diffResult.dispatchUpdatesTo(this)
}

class TimeSlotsDiffCallBack(
    var oldTimeSlotsList: List<TimeSlots>,
    var newTimeSlotsList: List<TimeSlots>
) : DiffUtil.Callback() {

    override fun getOldListSize(): Int = oldTimeSlotsList.size

    override fun getNewListSize(): Int = newTimeSlotsList.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return (oldTimeSlotsList[oldItemPosition].time
                == newTimeSlotsList[newItemPosition].time)
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        val oldTimeSlots = oldTimeSlotsList[oldItemPosition].status
        val newTimeSlots = newTimeSlotsList[newItemPosition].status
        Timber.e("old: $oldTimeSlots new: $newTimeSlots")
        return oldTimeSlots == newTimeSlots
    }
}

}

这是提交适配器列表的 UI 类。在 doClickTimeSlot() 下方,我正在更新项目的状态。

class TimeSlotBottomSheetFragment : BottomSheetDialogFragment(){
private val obj: List<TimeSlots> = mutableListOf<TimeSlots>(
    TimeSlots("09:30", "0"),
    TimeSlots("10:30", "0"),
    TimeSlots("11:30", "0"),
    TimeSlots("12:30", "0"),
    TimeSlots("14:30", "0"),
    TimeSlots("15:30", "0"),
    TimeSlots("16:30", "0"),
    TimeSlots("17:30", "0"),
    TimeSlots("18:30", "0"),
    TimeSlots("19:30", "0"),
    TimeSlots("20:30", "0"),
    TimeSlots("21:30", "0")
)

private fun doClickTimeSlot(timeSlots: TimeSlots) {
    val newList: ArrayList<TimeSlots> = ArrayList<TimeSlots>()
    newList.addAll(obj)
    for (i in newList.indices) {
        if (newList[i].time == timeSlots.time) {
            newList[i].status = "1"
            Timber.e("" + newList[i].time + " " + newList[i].status)
        } else {
            newList[i].status = "0"
            Timber.e("" + newList[i].time + " " + newList[i].status)
        }
    }
    adapter.submitSlots(newList)
}
}
4

0 回答 0