RecyclerView的notifyItemChanged,notifyItemAdd,notifyItemRemoved方法会有默认动画。
要屏蔽notifyItemChanged方法产生的动画,可使用以下方法:
((SimpleItemAnimator)mUserListView.getItemAnimator()).setSupportsChangeAnimations(false);
我查看了源码,没有发现有api可以屏蔽另外两个方法产生的动画。只能将DefaultItemAnimator复制一份,重新命名一下MyItemAnimator。然后对代码做以下修改:
@Override public boolean animateAdd(final RecyclerView.ViewHolder holder) { resetAnimation(holder); //ViewCompat.setAlpha(holder.itemView, 0); 这里注释掉 mPendingAdditions.add(holder); return true; }
在animateAddImpl方法中,将以下代码中的设置alpha的代码删除
animation.setDuration(getRemoveDuration())
.alpha(0)</span>.setListener()
在animateRemoveImpl方法中做同样的处理即可。