检测完成动画在Android的RecyclerView [英] Detect animation finish in Android's RecyclerView

查看:1257
本文介绍了检测完成动画在Android的RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RecyclerView ,不像到的ListView ,并没有一个简单的方法来一个空的视图设置为它,所以人们必须手动管理它,使空视图可见适配器的项目计数的情况下为0。

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.

实现这个,一开始我试图修改垫层结构之后有权要求空视图逻辑(的ArrayList 在我的情况),例如:

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();
    }
});

它做的事情,但有一个缺点:被移除的最后一个元素时,空视图的出现动画之前删除完毕后,取出后立即的。所以我决定要等到动画结束,然后更新UI。

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.

要我惊讶的是,我找不到听在RecyclerView动画事件的好方法。即将在脑海中的第一件事就是用 isRunning 的方法是这样的:

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();
            }
        });
    }
});

不幸的是,回调在这种情况下立即运行,因为在那一刻,内心 ItemAnimator 仍然没有在运行状态。所以,问题是:如何正确使用ItemAnimator.isRunning()方法并有更好的方式来达到预期的效果,即显示空视图后,单一元素的去除动画完成

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?

推荐答案

目前我已经找到了解决这个问题的唯一工作方式是延长ItemAnimator,并把它传递给RecyclerView是这样的:

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();
    }
});

但这种技术并不普遍,因为我已经从具体的实施ItemAnimator延长正在使用的RecyclerView。在里面CoolRecyclerView私有内部CoolItemAnimator的情况下,我的方法将无法工作。

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:我的同事建议包裹ItemAnimator的装饰以下面的方式:

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

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

这将是很好,尽管看起来矫枉过正这样一个简单的任务,但在这种情况下,创造了装饰是不可能的,无论如何,因为ItemAnimator有一个方法使用setListener(),这是包保护的,所以我显然不能把它包装,以及一些最后的方法。

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.

这篇关于检测完成动画在Android的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆