Simple Recyclerview Kotlin
Today we are going create the Recyclerview in Kotlin with very simple and fewer lines of code.
To create a Recyclerview we need to create
- Create a Simple Data class
- Create RecyclerAdapter
- Create ViewHolder
Create a Simple Data class
Create a data class SimpleAdapter.kt with the code below .just look at the Data class how kotlin helps us to reduce boilerplate code. Setter getter also not required here.
Create RecyclerView Adapter and ViewHolder
Create SimpleAdapter.kt file with the code below. This Kotlin file contains two classes, SimpleAdapter class which takes an ArrayList of SeriesModel and send it layout_cell.xml to show. And The ViewHolder will assign the values from the model to their corresponding views.
MainActivity
It is the main Kotlin Activity which creates the RecyclerView. In this class we create a simple ArrayList For Contact in a single line for loop. How Kotlin reduced boilerplate codes. This data are displayed in the list and then we pass this array of elements to MyAdapter class(the source code of which is right above), to display the content of our ArrayList.
The final output would be like this.sorry for the bad UI. Didn't spend much time on it.
If you have any questions or comments, feel free to comment below.
Will update the source code for GitHub soon here.
Check some other articles from me.