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

Pra*_*tre 5 android android-layout kotlin android-recyclerview

RecyclerView在我的应用程序中使用它有itemViewCache20 个项目,因为我的列表的最大可能大小为 20。它是“单个项目选择列表”。现在当列表加载时,第一个单元格将突出显示为默认选定位置。如果我单击不同位置的单元格,该位置的视图将被选中并突出显示,同时先前选择的视图变为白色,即未选中。以上工作正常,因为我使用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
}
Run Code Online (Sandbox Code Playgroud)

}

这是我如何从我的活动中调用更改,

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

Hon*_*uan 3

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

适配器不应假设有效负载将始终传递给 onBindViewHolder(),例如,当未附加视图时,有效负载将被简单地删除。

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

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