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

前端 未结 4 874
执笔经年
执笔经年 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.

    0 讨论(0)
  • 2020-12-24 06:43

    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

    0 讨论(0)
  • 2020-12-24 06:56

    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);
    
    0 讨论(0)
  • 2020-12-24 06:57

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

    0 讨论(0)
提交回复
热议问题