RecyclerView With Data Binding in Android Kotlin

RecyclerView With Data Binding in Android Kotlin: Example With Kotlin

RecyclerView is an advanced and flexible version of a ListView. We use RecyclerView when we have to display a large number of elements in one activity. If you don’t know how many views you have to load into the list, then RecyclerView is the best option for you. So, in this example we will learn how to create RecyclerView With Data Binding in Android using Kotlin.

 

RecyclerView is better than a ListView because it saves a lot of memory because it reuses the Views when you scroll the activity rather than creating the views in the beginning of the Activity. When all the views are loaded in the beginning it wastes memory because there are many views that are loaded but cannot be seen on the screen. So, this wastes the memory and makes the app heavier in size.

In Application development the memory cost feature is of great importance. Android system runs lots of apps so if your app is really heavy in size then it will not respond timely.

RecyclerView With Data Binding in Android Kotlin
RecyclerView With Data Binding in Android Kotlin

 

How RecyclerView works:  RecyclerView With Data Binding in Android Kotlin

RecyclerView works in three parts.

  • The Layout
  • ViewHolder
  • Adapter

The layout is created for each and every item to be loaded in the RecyclerView.

To save the memory ViewHolder is used because it caches the views.

The adapter fills the ViewHolders with data and returns the information of the data.

Lets start creating RecyclerView

In this example I will be showing a list of countries into a RecyclerView.

1- First of all create a new Android Studio Project with an empty Activity. Select KOTLIN as the default language for the project.

2- Name your project with “RecyclerViewKotlin” or you can name it whatever you want.

3- Now first of all come to your build.gradle (Module: app) file and then add this dependency for the RecyclerView.

 

 

This is the latest dependency for RecyclerView. If you’re using older versions of Android Studio then you should use version 26.1.0

You must have Kotlin’s Android extension and RecyclerView dependency in your

build.gradle (Module: app) before moving ahead.

build.gradle(module: app) file will look like this

 

 

4- Now, come to your “activity_main.xml” file and in the Code section, first change the default Constraint Layout to Relative Layout.

5- After that, add a RecyclerView with width “match_parent” and height “wrap_content”.

6- Give an Id to RecyclerView “recyclerview”.

7- So, the final “activity_main.xml” file will look like this

activity_main.xml

 

 

8- Now to design the look of each row in our RecyclerView we will create a new Layout file. Name this Layout file with “row_data”.

9- In your  “row_data” file, first change the default Layout to Relative Layout and then change the height of your Relative Layout to “wrap_content”. By doing so you will not get one TextView per page.

10- Add a TextView and then change it’s height to “wrap_content”. Then our final “row_data” file will look like this:

row_data

 

 

11- So, come to MainActivity_kt and create an ArrayList and add some name of countries in it.

12- Add a LinearLayout Manager. Layout Manager gives position to the views inside RecyclerView. Also it handles when to reuse the views that are not visible to the user on the screen.

13- We need a RecyclerView adapter to load data. We will name the adapter “RecyclerViewAdapter”. It will have two parameters: ArrayList of countries and context (this).

14- In next step we will create an adapter. Because of that you will face an error until the adapter is created.

MainActivity_kt

 

 

15- But In the same folder in which you have your MainActivity.kt file create a new Kotlin file with name “RecyclerViewAdapter”.

16- In your RecyclerViewAdapter file, create a class “RecyclerViewAdapter” which implements a RecyclerView Adapter and it takes two parameters: (Here you will see an error)

  • ArrayList of countries.
  • context.

17- Below the “RecyclerViewAdapter” class create a ViewHolder class. Here we will create a variable which will hold the TextView (country_names).

18- So, to remover the error you are facing press ALT+ENTER and override all the three functions of the class. These functions are:

  • getItemCount
  • onCreateViewHolder
  • onBindViewHolder

19- After this, in function getItemCount, we will return the number of countries in the list.

20- In onCreateViewHolder we inflate the row_data view which we are using to hold each item in our list.

21- In onBindViewHolder we bind the items of the list with TextViews.

22- So, our final “RecyclerViewAdapter” class will look like this:

RecyclerViewAdapter

 

 

After doing this code you will see a nice ListView like the one in the Output. If you want to customize the List then that can be done by using some methods. I hope you enjoyed this tutorial 🙂

 

OUTPUT

 

 

RecyclerView With Data Binding in Android Kotlin
RecyclerView. With Data Binding in Android Kotlin

 

RecyclerView With Data Binding in Android Kotlin
RecyclerView. With Data Binding in Android Kotlin

 

 

       People Also Read  

8 thoughts on “RecyclerView With Data Binding in Android Kotlin: Example With Kotlin”

  1. Pingback: Android Custom Toolbar In Kotlin | Toolbar Kotlin Example - My Android

  2. Pingback: Check Internet Connection Programmatically Android Kotlin - My Android

  3. Pingback: ListView In Android Using Kotlin: ListView Example in Kotlin - My Android

  4. Pingback: Key Differences Between JavaScript And Python - My Android

  5. Pingback: Kotlin Speech To Text Example | Step by Step Example - My Android

  6. Pingback: Kotlin vs java | Which One Is Better? Details and Comparison - My Android

Leave a Comment

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