2

I am a beginner in Android Development. When I search for RecyclerView animation, I have found about RecyclerView.ItemAnimator and but I can't find any tutorial on how to use this. I have read this How to use ItemAnimator in a RecyclerView?. But still, I don't get anything.

I have found documentation.

By default, RecyclerView uses DefaultItemAnimator.

What type of animation can expect from DefaultItemAnimator If I use RecyclerView?

I have used LayoutAnimation, When should I use RecyclerView.ItemAnimator?

Athira Reddy
  • 1,004
  • 14
  • 18

2 Answers2

2

You can use Any type of animation to animate your recycler view item

enter image description here

Required steps to build the following behavior

  1. Start off by creating the file item_animation_fall_down.xml in res/anim/ and add the following:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:duration="@integer/anim_duration_medium">
    
        <translate
            android:fromYDelta="-20%"
            android:toYDelta="0"
            android:interpolator="@android:anim/decelerate_interpolator"
            />
    
        <alpha
            android:fromAlpha="0"
            android:toAlpha="1"
            android:interpolator="@android:anim/decelerate_interpolator"
            />
    
        <scale
            android:fromXScale="105%"
            android:fromYScale="105%"
            android:toXScale="100%"
            android:toYScale="100%"
            android:pivotX="50%"
            android:pivotY="50%"
            android:interpolator="@android:anim/decelerate_interpolator"
            />
    
    </set>
    
  2. Defining the LayoutAnimation

With the item animation done it’s time to define the layout animation which will apply the item animation to each child in the layout. Create a new file called layout_animation_fall_down.xml in res/anim/ and add the following:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_animation_fall_down"
    android:delay="15%"
    android:animationOrder="normal"
    />
  1. Applying the LayoutAnimation

A LayoutAnimation can be applied both programmatically and in XML.

JAVA

int resId = R.anim.layout_animation_fall_down;
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ctx, resId);
recyclerview.setLayoutAnimation(animation);

XML

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"                                        
    android:layoutAnimation="@anim/layout_animation_fall_down"
    />

For more info visit here

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
  • then what is the use of ItemAnimator? When should I use ? – Athira Reddy Nov 21 '19 at 09:46
  • You can use ItemAnimator to apply default animation or create a class then extend ItemAnimator and apply your desired animation, after all, set created class as a default animation – Jasurbek Nov 21 '19 at 10:49
0

You wish to animate rows of recyclerview. Use below function

 public void setFadeAnimation(View view) {
    AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(1000);
    view.startAnimation(anim);
}

Now Go to Adapter class and call this function like this inside onBindViewHolder method

setFadeAnimation(myViewHolder.itemView);

It will animate your rows

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35