How to make Expandable RecyclerView using Kotlin
- December 01, 2019
- John Codeos
Today, I’m going to show you how to make an expandable RecyclerView with smooth animation without using 3rd party libraries.
We’re going to use a custom class named ExpandableLayout, created by SilenceDut.
It’s a custom LinearLayout class that handles the expand-collapse animations using the ValueAnimator.
Making Expandable RecyclerView
First, let’s add the custom class ExpandableLayout in the project.
Go to your package name on the left side and press right click > New > Kotlin File/Class
Give the name ExpandableLayout, choose Class and press OK.
Inside this file, paste the following code:
Now, go to res folder and right-click on the values folder > New > Values resource file.
In the new window, give the name attr and press OK
…and paste the following code:
Go to your Activity’s XML file and paste the following code to add a RecyclerView
And for the cell of the RecyclerView:
With ExpandableLayout you need to have 2 layouts inside. One for the cell, in this example is the Question, and one for the view that expand-collapse below the cell when the user clicks on it (Answer).
Set up your RecyclerView in your Activity (MainActivity.kt).
And for the RecyclerViewAdapter:
You can find the final project here
If you have any questions feel free to DM me on Twitter @johncodeos or leave a comment below!