It is pretty interesting to make to your app attractive and not making the user bored while using the app. Obviously, it’s the user interface and the performance of the app that makes the user like it.
In this blog, we will learn about adding animations to your Recyclerview items as they are created.
All you need to do is to add animations to each view holder inside Recyclerview Adapter’s onBindViewHolder Function.
1 2 |
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left); holder.itemView.startAnimation(animation); |
As you can see in the above code segment that we have created a variable animation and provided the context and animation which is to be used as the parameters. This animation is applied to the holder.itemView.
This will animate the view every time it is created.
However, If you want the animations to happen only once and not every time the view is created then you can follow the below code segment
1 2 3 4 5 6 7 |
int lastPosition = -1; // If the bound view wasn't previously displayed on screen, it's animated if (position > lastPosition) { Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left); holder.itemView.startAnimation(animation); lastPosition = position; } |
The above code segment compares the last position with the position of the view created and will onanimate the view if the lastPosition is lesser than the current item position.
That’s all you need to make your Recyclerview animate. Create you own customised animations and try different combinations like this
1 2 3 4 5 6 7 8 9 10 |
boolean animation = true; if (animation) { animation = false; Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left); holder.itemView.startAnimation(animation); } else { animation = true; Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_in_right); holder.itemView.startAnimation(animation); } |
This code will set the slide_in_left and slide_in_right animations alternatively on each view of the Recyclerview.
Thank you very much, this is Vedesh Kumar signing off.