Kotlin RecyclerView With data class (Kotlin)

Hi friends hope you are doing well . In this article i am gonna show you one of the most used view component in android. RecyclerView In Kotlin

This article will show your how to use kotlin data class  to create objects and to define custom hashcode and equals methods

First Step is to create an activity with support for kotlin

After that add in app dependencies

For simplicity i have added comment to explain  about code

My onCreate function

I am not using layout file Just Created a Recyclerview and set it as content for this activity .There is nothing special in it you can define it however you like.

–>after that created  dummy list with some user

–>As you can see while creating any object we dont need to add (new ) keyword before class name in kotlin

Now its time to have a look of our data class 

data classes is another reason to love kotlin. this class generate getter, setter, equal and hashcode for you

As i have already defined isSelected as false it is not compulsary to pass while creating object of user isn’t it great (For me its just another reason to ditch Java)

or if you want to provide different custom for hashcode generation and equal you can do it like this

Finally our RecyclerView Adapter

Here i am just creating a simple class which extending RecyclerView.Adapter and passing Viewholder in it (Common RecyclerView Work)

Thats all you need to make RecyclerView in Kotlin With Custom object list

Resources Used:

single_user_row (in layout)

bg_reactangle_selector.xml (in drawable)

Have a wonderfull day bye.

Leave a Reply

Your email address will not be published. Required fields are marked *