RecyclerView.Adapter.notifyItemChanged() never passes payload to onBindViewHolder()

前端 未结 4 873
执笔经年
执笔经年 2020-12-24 06:01

I\'m trying to update a ViewHolder in a RecyclerView without rebinding the entire thing. According to the docs, I should do this by calling R

4条回答
  •  礼貌的吻别
    2020-12-24 06:42

    If you use item animator (by default it's enabled) then payload will be always empty. Look at this answer in android issue tracker:

    This is how payload is designed the work. It is only passed to on bind "if we are rebinding to the same view holder". Same for data binding, if you are not binding to the same view holder, you cannot do the bind incrementally. In case of change animations, RV creates two copies of the View to crossfade so you are not binding to the same VH in postLayout.

    Soon, we'll provide an API to run change animations within the same ViewHolder.

    Therefore, until they share this new API we need to use something like this:

    mRecyclerView.setItemAnimator(null);
    

    edit: See @blindOSX comment, where he noticed that to enable payloads you can only disable animations of item change events.

    edit2: Looks like they've updated this behaviour without notice about it. See updated @Patricia Li answer.

提交回复
热议问题