Detect animation finish in Android's RecyclerView

后端 未结 9 1672
后悔当初
后悔当初 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 03:01

    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.

提交回复
热议问题