如何在像 Google plus/Google 报亭这样的滚动条上为 recyclerview 设置动画?

How to animate recyclerview on scroll like Google plus/Google newsstand?

如何在项目首次出现以及用户滚动时为 RecyclerView 设置动画,就像 google plus 应用程序或 google 新闻站应用程序。

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

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

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

 @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.

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

对于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>

https://github.com/wasabeef/recyclerview-animators

在我的代码中是这样的:

import jp.wasabeef.recyclerview.animators.adapters.AlphaInAnimationAdapter;

....

public function populate() {
   // Get your recicleview
   rv = (RecyclerView)findViewById(R.id.rv);
   rv.setHasFixedSize(true);

   // Populate your cursor with your own method...
   Cursor myRecycleItems= null;
   myRecycleItems= mDbHelper.getItems();

   //create your 
   itemsAdapter= new ItemsAdapter(myRecycleItems, getApplicationContext());


   //Finnaly apply your adapter to RV with AlphaInAnimationAdapter:
   rv.setAdapter(new AlphaInAnimationAdapter(itemsAdapter));

}

您需要将依赖项添加到 gradle

dependencies {
  // jCenter
  ...... 
  your curent dependencies 
  ....
  compile 'jp.wasabeef:recyclerview-animators:2.0.0'
}

阅读文档表格 https://github.com/wasabeef/recyclerview-animators 进行安装。

RecycleView.Adapter 方法中没有任何外部库 onBindViewHolder 使用动画,如示例:

       if (position>lastAnimatedPosition) {


        //set init transitionY to animate from it
        holder.itemView.setTranslationY(holder.itemView.getHeight());

        //animate to orginal position
        holder.itemView.animate().translationYBy(-  holder.itemView.getHeight()).start();


        lastAnimatedPosition=position;
    }

以上代码将从底部开始对列表中的每一行进行动画处理。动画只会执行一次,但是 onBindViewHolder 在滚动时 运行 所以第一次滚动列表会有动画效果。

初始化视图以开始动画非常重要,因此在示例中我设置:

  holder.itemView.setTranslationY( + Y change);

然后动画回到原来的位置:

 holder.itemView.animate().translationYBy(- Y change).start();

如果您需要 alpha,请这样做:

 holder.itemView.setAlpha(0);
 holder.itemView.animate().apha(1).start();