如何像 Google plus/Google 报亭那样在滚动上为 recyclerview 设置动画? [英] How to animate recyclerview on scroll like Google plus/Google newsstand?

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

问题描述

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

另外我在某处读到 RecyclerView 不直接支持用户滚动时的动画;如果这是真的,我们还有什么办法可以做到吗?

解决方案

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

更新:要修复快速滚动行为,请覆盖适配器的 onViewDetachedFromWindow 方法并在动画视图上调用 clearAnimation(在本例中为 holder.itemView.clearAnimation()代码> ).像这样:

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

up_from_bottom.xml

<翻译android:fromXDelta="0%" android:toXDelta="0%"android:fromYDelta="100%" android:toYDelta="0%"android:duration="400"/></set>

down_from_top.xml

<翻译android:fromXDelta="0%" android:toXDelta="0%"android:fromYDelta="-100%" android:toYDelta="0%"android:duration="400"/></set>

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

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

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.

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.

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>

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天全站免登陆