Athira Reddy
Athira Reddy

Reputation: 1054

how to Use RecyclerView.ItemAnimator With RecyclerView

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?

Upvotes: 2

Views: 4144

Answers (2)

Jasurbek
Jasurbek

Reputation: 2966

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

Upvotes: 2

Sanwal Singh
Sanwal Singh

Reputation: 1793

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

Upvotes: 0

Related Questions