RecyclerView:如何创建插入动画效果? [英] RecyclerView : How to create insert animation effect?

查看:1607
本文介绍了RecyclerView:如何创建插入动画效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ReyclerView 有工作一个 LinearLayoutManager 适配器< ViewHolder> 。我有一个项目列表,我想在与刀片(幻灯片)动画recyclerview显示。我怎么去呢?

I have a ReyclerView working with a LinearLayoutManager and an Adapter<ViewHolder>. I have a list of items I would like to display in the recyclerview with the insert (slide in) animation. How do I go about this ?

我想与基于项的指数线性增加延迟显示动画。

I would like to display the animations with a linearly increasing delay based on the index of the item.

目前,如果我用2个按钮,在recyclerview添加和删除,然后执行相应的操作( notifyItemInserted() notifyItemRemoved(),动画进来很好。

Currently, if I use 2 buttons, 'add' and 'remove', and then do the respective operations on the recyclerview (notifyItemInserted() and notifyItemRemoved(), the animations come in nicely.

如果我编程环路上的数据集,并添加物品,再次使用 notifyItemInserted(),我没有看到任何动画。我刚才看到的所有项目几乎出现一次。

If I programmatically loop on the data set and add the items, again, using notifyItemInserted(), I do not see any animation. I just see all the items appear almost at once.

如果我用Asynctasks线性延迟,然后添加/ OnPostExecute删除的项目(),我还没有看到任何动画。另外,我看到运行到死锁如果多个插入线程等待所有删除线程完成(有没有地方为删除线程来运行)的可能性。

If I use Asynctasks with linear delay, and then add/remove the item in OnPostExecute(), I still do not see any animation. Also, I see a possibility of running into deadlocks if multiple insert threads are waiting on all remove threads to be completed (with no place for the remove threads to run).

我是什么做错了吗?

我已完成大多数与此相关的上,这样的问题,并花了几天围绕recyclerview,仍然没有运气的动画部分戳去。

I have gone through most of the questions related to this on SO and have spent days poking around the animation part of the recyclerview, still no luck.

推荐答案

下面是我在我的适配器添加动画。这将动画一个推动作用,与该行从右边进入。

Below is how I add an animation in my Adapter. This will animate a push effect, with the row coming in from the right.

首先定义XML中的动画( RES /动画/ push_left_in.xml

First define the animation in xml (res/anim/push_left_in.xml)

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="300"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="300" />
</set>

然后将其设置在你的适配器为这样

Then set it in your Adapter as so

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        row = inflater.inflate(R.layout.music_list_item, null);
    } else {
        row = convertView;
    }

    ...

    //Load the animation from the xml file and set it to the row
    Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_in);
    animation.setDuration(500);
    row.startAnimation(animation);

    return row;
}

这个动画将会显示每次添加新行的时候,它应该在您的案件。

This animation will be displayed each time you add a new row, it should work in your case.

修改

这是你可以如何使用添加动画 RecyclerView

This is how you could add an animation using a RecyclerView

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
    holder.text.setText(items.get(position));

    // Here you apply the animation when the view is bound
    setAnimation(holder.container, position);
}

/**
 * Here is the key method to apply the animation
 */
private void setAnimation(View viewToAnimate, int position)
{
    // If the bound view wasn't previously displayed on screen, it's animated
    if (position > lastPosition)
    {
        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.push_left_in);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}

这篇关于RecyclerView:如何创建插入动画效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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