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

落花浮王杯 提交于 2019-11-30 01:35:13

RecyclerView by default creates another copy of the ViewHolder in order to fade the views into each other. This causes the problem because the old ViewHolder gets the payload but then the new one doesn't. So you need to explicitly tell it to reuse the old one:

DefaultItemAnimator animator = new DefaultItemAnimator() {
        @Override
        public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
            return true;
        }
    };
mRecyclerView.setItemAnimator(animator);
sosite

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.

In some case ,it worked with notifyItemRangeChanged(getItemRealCount(), 1).

why not simply use RecyclerView.Adapter.notifyItemChanged(int position) the docs seems a little ambiguous while mentioning about

  RecyclerView.Adapter.notifyItemChanged(int position,Object payload)

Client can optionally pass a payload for partial change. These payloads will be merged and may be passed to adapter's onBindViewHolder(ViewHolder, int, List)

hence it is not guaranteed that you will receive the List in your onBindViewHolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!