Sunday, October 22, 2017
Home > Tutorial for Beginners > Kotlin RecyclerView Example – Building a RecyclerView with Kotlin

Kotlin RecyclerView Example – Building a RecyclerView with Kotlin

Kotlin-RecyclerView-Example-AndroidGuruz.com

Lets learn building a RecyclerView using Kotlin in this Kotlin RecyclerView Example. Learning the small things will help you to migrate to Kotlin. And yes Kotlin will be surely easy and fun once you’ll get used to it. So lets see this Kotlin RecyclerView Example.

 

Kotlin RecyclerView Example

Creating a New Project

  • So first create a new Android Studio project using Kotlin.
  • Once the project is loaded come inside the activity_main.xml and remove the Hello World TextView.

Adding RecyclerView

  • Now search for RecyclerView and drag it to your activity_main.xml (See the image for help).

Kotlin RecyclerView Example

  • When you will drag RecyclerView it will ask you to add the RecyclerView dependency.

Kotlin RecyclerView Example

  • So the xml code for our activity_main.xml will be.

Creating List Layout using CardView

  • Now for the list items we will create a new layout. So create a new layout resource file and name itlist_layout.xml.
  • Now again to the same search CardView and drag it to the layout you just created. It will again ask you to add CardView dependency.Kotlin RecyclerView Example
    • Now for the layout use the following xml code. Here I am going to display name and address only. But you can also design anything you want for your list.
    • Now thats it for the List Layout. The above xml code will generate the following layout.

    Kotlin RecyclerView Example

    Kotlin RecyclerView Example

    • Now lets create data class for list model.

    Creating Model

    • The list is about user so we will create a User class.
    • Create a new Kotlin file and write the following code.
    • Now lets build the Adapter.

    Creating RecyclerView Adapter

    • If you have already created a RecyclerView in java, then you may already know that we need an Adapter to display the data in RecyclerView.
    • So create a new Kotlin class named CustomAdapter.kt and write the following code.
    • Now the last step is creating the RecyclerView itself.

    Creating RecyclerView

    • Now come to MainActivity.kt and write the following code.
    • Now thats it for the coding. Lets try running the application.

    Kotlin RecyclerView Example

    Kotlin RecyclerView Example

    • As you can see it is working absolutely fine.

    So thats all for this Kotlin RecyclerView Example friends. If you are having any confusions and queries then lets discuss it on the comment section. And I will keep posting more tutorials using Android Development with Kotlin as well. And for your side I want you to SHARE this post if you think it is useful. Thank You 🙂

 

 

 

Leave a Reply

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