Adding animations in the RecyclerView items: Step 2

Now it's time to add the sliding animation. As we said, we are going to add them from an XML file. Since all that we need is to apply a translation animation, we are going to use a View animation. Go ahead and create a translate animation that will have a duration of 700ms. Then you should load the animation from the CounterAdapter and apply it to your view.

Give it a few tries before peeking!!!

Hints:

Tween animations should be under the res>anim directory

DeltaX is the changing variable, while DeltaY should remain the same since we don't want to move our view also on

We need to load the animation using AnimationUtilsclass.

(https://developer.android.com/guide/topics/graphics/view-animation#kotlin)

We need to apply the animation to our parent view of the list item

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val avatar = itemView.iv_added_item
    val container = itemView.added_item_container
}

Each item View has an animation property.

val slideAnimation = ...
holder.itemView.animation = slideAnimation
// container transition to end
// reset container transition (transition to start)

We want to apply the animation only for the last item, so we need to check what is the last position and the current position before applying the animation.

Last updated