如何在Google Plus/Google报亭等滚动动画上实现recyclerview的动画? [英] How to animate recyclerview on scroll like Google plus/Google newsstand?

查看:65
本文介绍了如何在Google Plus/Google报亭等滚动动画上实现recyclerview的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当项目首次出现时以及用户滚动时,该如何为RecyclerView设置动画,就像它在google plus应用程序或google news stand应用程序中的工作方式一样.

How do I animate the RecyclerView when the items appear for first time and also when the user scrolls, just like the way it works for the google plus app or the google news stand app.

我还读到某处内容,当用户滚动时,RecyclerView不直接支持动画.如果这是真的,我们还有什么办法可以做到?

Also I read somewhere that RecyclerView does not directly support animation when the user scrolls; if this is true, is there any way we can still do it?

推荐答案

我是这样做的.可能会帮助某人.我不知道这是否是最好的方法,但是对我来说很好.

I did it this way. Might help someone. I don't know whether it's the best way to do it but works fine for me.

更新: 要解决快速滚动行为,请重写适配器的onViewDetachedFromWindow方法,然后在动画视图(在本例中为holder.itemView.clearAnimation())上调用clearAnimation.

UPDATE: To fix fast scrolling behaviour, override onViewDetachedFromWindow method of the adapter and call clearAnimation on the animated view (in this case, holder.itemView.clearAnimation() ).Like this:

 @Override
public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    holder.itemView.clearAnimation();
}

up_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="@android:anim/decelerate_interpolator">
<translate
    android:fromXDelta="0%" android:toXDelta="0%"
    android:fromYDelta="100%" android:toYDelta="0%"
    android:duration="400" />
</set>

down_from_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="@android:anim/decelerate_interpolator">
<translate
    android:fromXDelta="0%" android:toXDelta="0%"
    android:fromYDelta="-100%" android:toYDelta="0%"
    android:duration="400" />
</set>

最后将这段代码放在recyclerViewonBindViewHolder中.创建一个名为lastPosition的字段,并将其初始化为-1.

And finally put this code in onBindViewHolder of recyclerView. Create a field called lastPosition and initialize it to -1.

Animation animation = AnimationUtils.loadAnimation(context,
            (position > lastPosition) ? R.anim.up_from_bottom
                    : R.anim.down_from_top);
    holder.itemView.startAnimation(animation);
    lastPosition = position;

这篇关于如何在Google Plus/Google报亭等滚动动画上实现recyclerview的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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