如何实现RecyclerView的ItemAnimator禁用notifyItemChanged的动画 [英] How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged
问题描述
在我的项目中,我需要在 notifyItemChanged
时禁用 RecyclerView
的更改"动画.
我在RecyclerView
的源代码中进行了调查,并重写了android.support.v7.widget.DefaultItemAnimator
如下:
私有静态类ItemAnimator扩展了DefaultItemAnimator{@覆盖public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {if(oldHolder != null){oldHolder.itemView.setVisibility(View.INVISIBLE);dispatchChangeFinished(oldHolder, true);}if(newHolder != null){dispatchChangeFinished(newHolder, false);}返回假;}}
但我不确定我是否符合 Google 文档的规范:RecyclerView.ItemAnimator.animateChange
根据我对源代码的理解,如果我没有正确覆盖该方法,oldHolder不会被回收.
请帮我弄清楚如何以正确的方式覆盖animateChange
.
我找到了删除 animateChange 的正确解决方案.
很简单.Google 已实现该功能.
((SimpleItemAnimator) RecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
文档:setSupportsChangeAnimations>
In my project I need disable the "change" animation of RecyclerView
while notifyItemChanged
.
I investigated in the source of RecyclerView
and had overridden android.support.v7.widget.DefaultItemAnimator
as below:
private static class ItemAnimator extends DefaultItemAnimator
{
@Override
public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {
if(oldHolder != null)
{
oldHolder.itemView.setVisibility(View.INVISIBLE);
dispatchChangeFinished(oldHolder, true);
}
if(newHolder != null)
{
dispatchChangeFinished(newHolder, false);
}
return false;
}
}
But I am not sure if I match the spec of the Google document:
RecyclerView.ItemAnimator.animateChange
According to my understanding source code, if I do not override the method properly, the oldHolder will not be recycled.
Please help me figure out how to override animateChange
in a correct way.
I have found the correct solution to just remove the animateChange.
It's very simple. Google has implemented the functionality.
((SimpleItemAnimator) RecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
Documentation: setSupportsChangeAnimations
这篇关于如何实现RecyclerView的ItemAnimator禁用notifyItemChanged的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!