我尝试更新ViewHolder
中的RecyclerView
而不重新绑定整个内容。根据文档,我应该通过调用RecyclerView.Adapter.notifyItemChanged(int position, Object payload)
来执行此操作,其中payload
是一个将传递给RecyclerView.Adapter.onBindViewHolder(VH holder, int position, List<Object> payloads)
的任意对象,我可以在其中更新ViewHolder
但是当我尝试这个时,onBindViewHolder
总是收到一个空列表。在这两个调用之间,内部有效负载列表被清除。在RecyclerView的源代码中设置断点后,会发生这种情况,因为重新布局会最终调用RecyclerView.ViewHolder.clearPayload()
有没有其他人设法让这个工作?这是支持库中的错误还是我在触发这两个函数之间的重新布局时所做的事情?
这里是清除有效负载时的堆栈跟踪:
"<1> 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)
答案 0 :(得分:29)
RecyclerView
默认为ViewHolder
的{{3}},以便将视图淡化为彼此。这会导致问题,因为旧的ViewHolder
获取有效负载,但新的有效负载没有。因此,您需要明确告诉它重用旧的:
DefaultItemAnimator animator = new DefaultItemAnimator() {
@Override
public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
return true;
}
};
mRecyclerView.setItemAnimator(animator);
答案 1 :(得分:12)
如果使用项目动画(默认情况下已启用),则有效负载将始终为空。看看this answer in android issue tracker:
这就是有效负载的设计方式。它只传递给bind&#34;如果我们重新绑定到同一个视图持有者&#34;。 对于数据绑定也是如此,如果您没有绑定到同一个视图持有者,则无法以增量方式进行绑定。如果更改动画,RV会创建两个View to crossfade副本,因此您不会绑定到postLayout中的相同VH。
很快,我们将提供一个API来在同一个内部运行更改动画 ViewHolder。
因此,在他们分享这个新API之前,我们需要使用以下内容:
mRecyclerView.setItemAnimator(null);
编辑: 请参阅@blindOSX评论,他注意到要启用有效负载,您只能禁用项目更改事件的动画。
EDIT2: 看起来他们已经更新了此行为,恕不另行通知。查看更新的@Patricia Li answer。
答案 2 :(得分:1)
在某些情况下,它适用于notifyItemRangeChanged(getItemRealCount(), 1)
。
答案 3 :(得分:0)
为什么不简单地使用 RecyclerView.Adapter.notifyItemChanged(int position,Object payload)
文档在提及
{{1}}
客户端可以选择传递有效负载以进行部分更改。这些 有效负载将合并,可能传递给适配器 onBindViewHolder(ViewHolder,int,List)
因此无法保证您将在onBindViewHolder中收到List