Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect animation finish in Android's RecyclerView

The RecyclerView, unlike to ListView, doesn't have a simple way to set an empty view to it, so one has to manage it manually, making empty view visible in case of adapter's item count is 0.

Implementing this, at first I tried to call empty view logic right after modifying underlaying structure (ArrayList in my case), for example:

btnRemoveFirst.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View view) {         devices.remove(0); // remove item from ArrayList         adapter.notifyItemRemoved(0); // notify RecyclerView's adapter         updateEmptyView();     } }); 

It does the thing, but has a drawback: when the last element is being removed, empty view appears before animation of removing is finished, immediately after removal. So I decided to wait until end of animation and then update UI.

To my surprise, I couldn't find a good way to listen for animation events in RecyclerView. First thing coming to mind is to use isRunning method like this:

btnRemoveFirst.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View view) {         devices.remove(0); // remove item from ArrayList         adapter.notifyItemRemoved(0); // notify RecyclerView's adapter         recyclerView.getItemAnimator().isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {             @Override             public void onAnimationsFinished() {                 updateEmptyView();             }         });     } }); 

Unfortunately, callback in this case runs immediately, because at that moment inner ItemAnimator still isn't in the "running" state. So, the questions are: how to properly use ItemAnimator.isRunning() method and is there a better way to achieve the desired result, i.e. show empty view after removal animation of the single element is finished?

like image 488
Roman Petrenko Avatar asked Nov 14 '15 16:11

Roman Petrenko


Video Answer


2 Answers

I have a little bit more generic case where I want to detect when the recycler view have finished animating completely when one or many items are removed or added at the same time.

I've tried Roman Petrenko's answer, but it does not work in this case. The problem is that onAnimationFinished is called for each entry in the recycler view. Most entries have not changed so onAnimationFinished is called more or less instantaneous. But for additions and removals the animation takes a little while so there it's called later.

This leads to at least two problems. Assume you have a method called doStuff() that you want to run when the animation is done.

  1. If you simply call doStuff() in onAnimationFinished you will call it once for every item in the recycler view which might not be what you want to do.

  2. If you just call doStuff() the first time onAnimationFinished is called you may be calling this long before the last animation has been completed.

If you could know how many items there are to be animated you could make sure you call doStuff() when the last animation finishes. But I have not found any way of knowing how many remaining animations there are queued up.

My solution to this problem is to let the recycler view first start animating by using new Handler().post(), then set up a listener with isRunning() that is called when the animation is ready. After that it repeats the process until all views have been animated.

void changeAdapterData() {     // ...     // Changes are made to the data held by the adapter     recyclerView.getAdapter().notifyDataSetChanged();      // The recycler view have not started animating yet, so post a message to the     // message queue that will be run after the recycler view have started animating.     new Handler().post(waitForAnimationsToFinishRunnable); }  private Runnable waitForAnimationsToFinishRunnable = new Runnable() {     @Override     public void run() {         waitForAnimationsToFinish();     } };  // When the data in the recycler view is changed all views are animated. If the // recycler view is animating, this method sets up a listener that is called when the // current animation finishes. The listener will call this method again once the // animation is done. private void waitForAnimationsToFinish() {     if (recyclerView.isAnimating()) {         // The recycler view is still animating, try again when the animation has finished.         recyclerView.getItemAnimator().isRunning(animationFinishedListener);         return;     }      // The recycler view have animated all it's views     onRecyclerViewAnimationsFinished(); }  // Listener that is called whenever the recycler view have finished animating one view. private RecyclerView.ItemAnimator.ItemAnimatorFinishedListener animationFinishedListener =         new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {     @Override     public void onAnimationsFinished() {         // The current animation have finished and there is currently no animation running,         // but there might still be more items that will be animated after this method returns.         // Post a message to the message queue for checking if there are any more         // animations running.         new Handler().post(waitForAnimationsToFinishRunnable);     } };  // The recycler view is done animating, it's now time to doStuff(). private void onRecyclerViewAnimationsFinished() {     doStuff(); } 
like image 33
nibarius Avatar answered Oct 12 '22 11:10

nibarius


Currently the only working way I've found to solve this problem is to extend ItemAnimator and pass it to RecyclerView like this:

recyclerView.setItemAnimator(new DefaultItemAnimator() {     @Override     public void onAnimationFinished(RecyclerView.ViewHolder viewHolder) {         updateEmptyView();     } }); 

But this technique is not universal, because I have to extend from concrete ItemAnimator implementation being used by RecyclerView. In case of private inner CoolItemAnimator inside CoolRecyclerView, my method will not work at all.


PS: My colleague suggested to wrap ItemAnimator inside the decorator in a following manner:

recyclerView.setItemAnimator(new ListenableItemAnimator(recyclerView.getItemAnimator())); 

It would be nice, despite seems like overkill for a such trivial task, but creating the decorator in this case is not possible anyway, because ItemAnimator has a method setListener() which is package protected so I obviously can't wrap it, as well as several final methods.

like image 165
Roman Petrenko Avatar answered Oct 12 '22 10:10

Roman Petrenko