如何为ListAdapter禁用动画

时间:2018-11-05 08:51:04

标签: android android-recyclerview listadapter

我正在使用新的ListAdapter,它会自动为更改添加动画效果。我想禁用动画或以编程方式启用/禁用它。

class UserAdapter extends ListAdapter<User, UserViewHolder> {
     public UserAdapter() {
         super(User.DIFF_CALLBACK);
     }
     @Override
     public void onBindViewHolder(UserViewHolder holder, int position) {
         holder.bindTo(getItem(position));
     }
     public static final DiffUtil.ItemCallback<User> DIFF_CALLBACK =
             new DiffUtil.ItemCallback<User>() {
         @Override
         public boolean areItemsTheSame(
                 @NonNull User oldUser, @NonNull User newUser) {
             // User properties may have changed if reloaded from the DB, but ID is fixed
             return oldUser.getId() == newUser.getId();
         }
         @Override
         public boolean areContentsTheSame(
                 @NonNull User oldUser, @NonNull User newUser) {
             // NOTE: if you use equals, your object must properly override Object#equals()
             // Incorrectly returning false here will result in too many animations.
             return oldUser.equals(newUser);
         }
     }
 }

2 个答案:

答案 0 :(得分:2)

您可以尝试在setSupportsChangeAnimations项目动画师上使用RecyclerView禁用或启用动画:

SimpleItemAnimator itemAnimator = (SimpleItemAnimator) recyclerView.getItemAnimator();
itemAnimator.setSupportsChangeAnimations(false);

答案 1 :(得分:0)

另一种解决方案是简单地完全删除项目动画制作器。

recyclerView.itemAnimator = null