Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable animation for ListAdapter

Tags:

I am using new ListAdapter, which automatically animates changes. I would like to disable animations or enable/disable it programmatically.

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);          }      }  } 
like image 558
Michalsx Avatar asked Nov 05 '18 08:11

Michalsx


People also ask

How do I turn off screen animation?

Open Settings . Scroll down and select Accessibility. Scroll down to Display and tap Text and display. Tap the toggle switch for Remove animations to turn it on.

How do I turn off reduce animation?

In macOS: System Preferences > Accessibility > Display > Reduce motion. In iOS: Settings > General > Accessibility > Reduce Motion. In Android 9+: Settings > Accessibility > Remove animations.


2 Answers

Another solution is to simply remove the item animator altogether.

recyclerView.itemAnimator = null 
like image 155
Nathan Reline Avatar answered Sep 30 '22 05:09

Nathan Reline


You could try to disable or enable animations with setSupportsChangeAnimations on RecyclerView item animator:

SimpleItemAnimator itemAnimator = (SimpleItemAnimator) recyclerView.getItemAnimator(); itemAnimator.setSupportsChangeAnimations(false); 
like image 38
Aaron Avatar answered Sep 30 '22 04:09

Aaron