In this tutorial, we’ll learn how to create animation for a RecyclerView in which elements are displayed in the form of a Grid.

We’ve already discussed RecyclerView Layout Animations earlier using a List.

RecyclerView Grid Animation

In the previous tutorial, we created List based Layout Animations. At the end of the tutorial, we applied them to a Grid Layout and saw that the animation is still list based on the grid instead of row and column based.

In order to show Grid-based animation we need to use <gridlayoutanimation tag.
But this won’t work on a normal RecyclerView. It will crash since by default a RecyclerView doesn’t know anything about how the elements are placed by the Layout Manager and assumes a list based placement.

Hence we need to create a custom RecyclerView first, overriding the important methods.

Let’s get started with our application.

Project Structure

android-grid-layout-animation-project

Code

The code for the CustomGridRecyclerView.java class is given below:

In the above code, we’ve explicitly set the RecyclerView to use grids row and columns for animating.

The code for the activity_main.xml is given below:

The code for the MainActivity.java is given below:

The animation set is defined in the anim folder in the file down_to_up.xml.

The grid layout animation is defined in the file: gridlayout_animation_from_bottom.xml.

The row and column delay attributes are used to define the delay in animation in each row and column respectively after the previous row got animated.

The code for the RecyclerViewAdapter.java class is given below:

The output of the above application in action is given below:

android recyclerview gridlayout animation output

That brings an end to this tutorial. You can download the complete project from the link below:

By admin

Leave a Reply

%d bloggers like this: