Detect animation finish in Android's RecyclerView

后端 未结 9 1707
后悔当初
后悔当初 2020-12-29 02:31

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 vi

9条回答
  •  误落风尘
    2020-12-29 02:42

    To expand on Roman Petrenko's answer, I don't have a truly universal answer either, but I did find the Factory pattern to be a helpful way to at least clean up some of the cruft that is this issue.

    public class ItemAnimatorFactory {
    
        public interface OnAnimationEndedCallback{
            void onAnimationEnded();
        }
        public static RecyclerView.ItemAnimator getAnimationCallbackItemAnimator(OnAnimationEndedCallback callback){
            return new FadeInAnimator() {
                @Override
                public void onAnimationFinished(RecyclerView.ViewHolder viewHolder) {
                    callback.onAnimationEnded();
                    super.onAnimationEnded(viewHolder);
                }
            };
        }
    }
    

    In my case, I'm using a library which provides a FadeInAnimator that I was already using. I use Roman's solution in the factory method to hook into the onAnimationEnded event, then pass the event back up the chain.

    Then, when I'm configuring my recyclerview, I specify the callback to be my method for updating the view based on the recyclerview item count:

    mRecyclerView.setItemAnimator(ItemAnimatorFactory.getAnimationCallbackItemAnimator(this::checkSize));
    

    Again, it's not totally universal across all any and all ItemAnimators, but it at least "consolidates the cruft", so if you have multiple different item animators, you can just implement a factory method here following the same pattern, and then your recyclerview configuration is just specifying which ItemAnimator you want.

提交回复
热议问题