RecyclerView.Adapter.notifyItemChanged()永远不会将有效负载传递给onBindViewHolder()

我正在尝试更新RecyclerViewViewHolder而不重新绑定整个事物。 根据文档,我应该通过调用RecyclerView.Adapter.notifyItemChanged(int position, Object payload)来做到这一点,其中payload是一个任意对象,将被传递给RecyclerView.Adapter.onBindViewHolder(VH holder, int position, List payloads) ,我将能够更新ViewHolder。

但是当我尝试这个时, onBindViewHolder总是会收到一个空列表。 在这两个调用之间,内部有效负载列表被清除。 在RecyclerView的源代码中设置断点后,会发生这种情况,因为重新布局,最终调用RecyclerView.ViewHolder.clearPayload()

有没有其他人设法让这个工作? 这是支持库中的错误还是我在触发这两个函数之间的重新布局时所做的事情?

这是清除有效负载时的堆栈跟踪:

 " main@831692616832" prio=5 runnable java.lang.Thread.State: RUNNABLE at android.support.v7.widget.RecyclerView$ViewHolder.clearPayload(RecyclerView.java:8524) at android.support.v7.widget.RecyclerView$ViewHolder.resetInternal(RecyclerView.java:8553) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4544) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461) at android.support.v7.widget.LayoutState.next(LayoutState.java:86) at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1423) at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:610) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:581) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:532) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Method.java:-1) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(NativeStart.java:-1) 

Solutions Collecting From Web of "RecyclerView.Adapter.notifyItemChanged()永远不会将有效负载传递给onBindViewHolder()"

RecyclerView默认情况下会创建 ViewHolder 另一个副本 ,以便将视图相互淡化。 这会导致问题,因为旧的ViewHolder获取有效负载,但新的有效负载没有。 所以你需要明确告诉它重用旧的:

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

如果您使用项目animation(默认情况下已启用),则有效负载将始终为空。 在android问题跟踪器中查看此答案 :

这就是有效载荷的设计方式。 它只传递给绑定“如果我们重新绑定到同一个视图持有者”。 对于数据绑定也是如此,如果您没有绑定到同一个视图持有者,则无法以递增方式执行绑定。 如果更改animation,RV会创建两个View to crossfade副本,因此您不会绑定到postLayout中的相同VH。

很快,我们将提供一个API来在同一个ViewHolder中运行更改animation。

因此,在他们共享这个新API之前,我们需要使用以下内容:

 mRecyclerView.setItemAnimator(null); 

编辑:请参阅@blindOSX评论,他注意到要启用有效负载,您只能禁用项目更改事件的animation。

edit2:看起来他们已经更新了这种行为而没有注意到它。 查看更新的@Patricia Li答案 。

在某些情况下,它适用于notifyItemRangeChanged(getItemRealCount(), 1)

为什么不简单地使用RecyclerView.Adapter.notifyItemChanged(int position)文档似乎有点含糊不清同时提到

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

客户端可以选择传递有效负载以进行部分更改 这些有效负载将被合并,并可以传递给适配器的onBindViewHolder(ViewHolder,int,List)

因此无法保证您将在onBindViewHolder中收到List