gpt4 book ai didi

如果该位置的 View 在屏幕上不可见或滚出,则 Android Recyclerview notifyItemChanged(position ,payLoad) 不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:39:27 27 4
gpt4 key购买 nike

我在我的应用程序中使用 RecyclerView,它有 20 个项目的 itemViewCache 因为我的列表的最大可能大小是 20。它是“单项选择列表” .现在,当列表加载时,第一个单元格会突出显示为默认选定位置。如果我单击不同位置的单元格,该位置的 View 将被选中并突出显示,同时先前选中的 View 变为白色即未选中。以上工作正常,因为我使用 adapter.notifyDataSetChanged(position, payLoad) 方法更新列表中的数据。但是当当前选定位置的单元格滚出屏幕并且我尝试选择新位置时,但是新单元格捕获突出显示并被选中,它不会使先前选定的单元格变白。对于滚出位置的单元格,系统甚至不调用 notifyDataSetChanged(positioin, payLoad) 方法在其中制作 ui。任何帮助将不胜感激,因为我经常使用 RecyclerViews

这是我的适配器的代码:

class EventsCalendarAdapter(var eventSectionedFrag: EventSectionedFrag, var arrayList: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_calendar_months, parent, false)
return CalendarHolder(view)
}

override fun getItemCount() = arrayList.size


override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {
if (payloads.isNotEmpty()) {
var bundle = payloads[0] as Bundle
var isSelected: Boolean? = bundle.getBoolean("selected")
var setCurrentMonthSelected: Boolean? = bundle.getBoolean("setMonth")


with(holder) {
if (isSelected != null) {
val tvMonth = itemView.findViewById<TextView>(R.id.tv_month)
tvMonth.apply {
setTextColor(ContextCompat.getColor(eventSectionedFrag.activity!!, if (isSelected) R.color.white else R.color.better_than_black))
background.setColorFilter(ContextCompat.getColor(eventSectionedFrag.activity!!, if (isSelected) android.R.color.holo_red_light else R.color.white), PorterDuff.Mode.SRC)
}
}

if (setCurrentMonthSelected != null) {
val view = itemView.findViewById<View>(R.id.view_current)
view.visibility = if (setCurrentMonthSelected) {
View.VISIBLE
} else {
View.INVISIBLE
}
}
}
} else {
super.onBindViewHolder(holder, position, payloads)
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val myHolder = holder as CalendarHolder
myHolder.bindItems(eventSectionedFrag = eventSectionedFrag, month = arrayList[position], position = position)
}

class CalendarHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(eventSectionedFrag: EventSectionedFrag, month: String, position: Int) {
val tvMonth = itemView.findViewById<TextView>(R.id.tv_month)
val cardMonth = itemView.findViewById<ConstraintLayout>(R.id.card_month)
val view = itemView.findViewById<View>(R.id.view_current)
view.visibility = View.INVISIBLE
val callbacks = eventSectionedFrag as CalendarCallbacks

tvMonth.text = month
view.apply {
visibility = if (callbacks.getCurrentMonthPosition() == position) {
View.VISIBLE
} else {
View.INVISIBLE
}
}
cardMonth.setOnClickListener {
callbacks.onMonthSelected(adapterPosition, tvMonth)
}
}
}

fun addMonths(arrayList: ArrayList<String>) {
this.arrayList.clear()
this.arrayList.addAll(arrayList)
notifyDataSetChanged()
}

interface CalendarCallbacks {
fun onMonthSelected(position: Int, currentSelectedView: TextView)
fun getCurrentMonthPosition(): Int
}

下面是我如何从我的 Activity 中调用更改,

override fun notifyCalendar(isCurrentYearSelected: Boolean, position: Int) {
var bundle = Bundle()
bundle.putBoolean("setMonth", isCurrentYearSelected)
calendarAdapter.notifyItemChanged(position, bundle)
}

最佳答案

来自RecyclerView.Adapter#notifyItemChanged(int position, java.lang.Object payload) docs :

Adapter should not assume that the payload will always be passed to onBindViewHolder(), e.g. when the view is not attached, the payload will be simply dropped.

因此,当当前选定位置的单元格滚出屏幕时,不会发生任何变化,因为旧的选定 View 不存在。

您可以修改您的arrayList 以在其中存储选定的状态,或者向CalendarCallbacks 添加一个方法来获取当前选定的位置,但您必须注意在需要时更改所选位置。

关于如果该位置的 View 在屏幕上不可见或滚出,则 Android Recyclerview notifyItemChanged(position ,payLoad) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50966043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com