xorum.io
Published in

xorum.io

[Android] RecyclerView animated transition between Grid and List layouts.

If you have a RecyclerView that can display data both in a grid and a list you may think about making transition between them animated. And this article shows how to achieve this easily 🙂.

  1. Assuming that you already have RecyclerView your layout should look something like this:

2. First of all we need to initialise GridLayoutManager for our RecyclerView. I have initialisation code in onCreate() function of my activity.

3. The next step is to create adapter. I’ve written `SimpleAdapter` class that has link to layoutManager of our recycler view.

And attach it to RecyclerView in onCreate() of activity. Now it should look like this:

4. The last but not the least step — create 2 view holders for different modes.

and

Layout for simple_item.xml :

Layout for detailed_item.xml :

We’ve finished with the preparatory work and now the magic begins

To make recycler view animated we need to change the spanCount in our layout manager and call adapter.notifyItemRangeChanged() for all adapter items. I have button in menu so my code to handle layout changes looks like this:

And in adapter create the different view holders depending on mode:

Voila! You have animated transition between grid and list modes in recycler view 😀.

The whole project can be found here — https://github.com/alla-dubovska/animation-example

--

--

Kotlin Multiplatform solutions for Mobile (iOS | Android) and Web (Ktor | React.js)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alla Dubovska

Software engineer, Marathon finisher 🏃🏻‍♀️ Building mobile products 👩‍💻(native Android/iOS development).