如何实现RecyclerView的ItemAnimator禁用的动画notifyItemChanged [英] How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged
问题描述
在我的项目,我需要禁用变更
的 RecyclerView
动画,而 notifyItemChanged
。
In my project I need disable the "change
" animation of RecyclerView
while notifyItemChanged
.
我调查 RecyclerView
的源代码中,并覆盖 android.support.v7.widget.DefaultItemAnimator
如下:
I investigated in the source of RecyclerView
and override 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;
}
}
但我不知道我是否符合谷歌文档的规范: <一href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemAnimator.html#animateChange(android.support.v7.widget.RecyclerView.ViewHolder,%20android.support.v7.widget.RecyclerView.ViewHolder,%20int,%20int,%20int,%20int)">RecyclerView.ItemAnimator.animateChange
But I am not sure if I match the spec of the Google document: RecyclerView.ItemAnimator.animateChange
据我了解源$ C $ C,如果我不覆盖的方法得当,oldHolder的不会回收。
According to my understanding source code, if I do not override the method properly, the oldHolder will not recycled.
请帮我找出如何重写 animateChange
以正确的方式。
Please help me figure out how to override animateChange
in a correct way.
感谢。
推荐答案
我已经找到了正确的解决方案,只是删除animateChange。
I have found the correct solution to just remove the animateChange.
这很简单。谷歌已经实现的功能。
It's very simple. Google has implemented the functionality.
RecyclerView.getItemAnimator().setSupportsChangeAnimations(false);
文件:<一href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemAnimator.html#setSupportsChangeAnimations%28boolean%29">setSupportsChangeAnimations
这篇关于如何实现RecyclerView的ItemAnimator禁用的动画notifyItemChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!