RecyclerView - How to implement RecyclerView in Android using Kotlin.
Last updated Dec 14, 2021In this example, you will know how to implement RecyclerView in Android using Kotlin. RecyclerView is a more advanced and adaptable version of the listview and gridview. It's a container for presenting large amounts of data sets in a way that allows them to be browsed quickly while limiting the number of views. It has the ability to reuse its views.
With RecyclerView we can arrange the items in Vertical and horizontal directions using RecyclerView LayoutManager
Implementation:
Create a new Project in android studio.
Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish |
Add the recyclerview and cardview dependency to build.gradle(app)
implementation 'androidx.recyclerview:recyclerview:1.2.0' |
Add CardView Dependency if needed (No need to add In latest version of android studio)
implementation 'androidx.cardview:cardview:1.0.0' |
Go to activity_recycler_view.xml file and change the constraint layout to Linear Layout as follows:
<LinearLayout |
Add recyclerview widget inside LinearLayout.
<androidx.recyclerview.widget.RecyclerView |
Create a new layout resource file and add the following code
app > res > layout > new > layout resource file > enter name (cardview_layout.xml) > ok |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout <ImageView <TextView </LinearLayout> </androidx.cardview.widget.CardView> |
Create a new Kotlin class for model
app > java > package name > right-click > New > Kotlin class/file > enter name > OK |
This file will hold the information of every item which you want to show in your RecyclerView.
class RecyclerModel(val image : Int, val text: String) { } |
Create a adapter class
app > java > package name > right-click > New > Kotlin class/file > enter name > ok |
This class contains some important functions to work with the RecyclerView these are as follows:
> onCreateViewHolder(): This function sets the views to display the items.
> onBindViewHolder(): This function is used to bind the list items to our widgets such as TextView, ImageView, etc.
> getItemCount(): It returns the count of items present in the list.
import android.view.LayoutInflater class RecyclerAdapter(private val dataList: List) : RecyclerView.Adapter() { override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) { // Image set to the imageview widget // Text set to the textview widget override fun getItemCount(): Int { } |
Go to RecyclerViewActivity.kt file and add the following code
val recyclerview = findViewById(R.id.recyclerview) recyclerview.layoutManager = LinearLayoutManager(this) for (i in 1..12) {
recyclerview.adapter = adapter |
Now run the app in your emulator, and get the following output.
OUTPUT:
Complete source code of RecyclerView in android:
activity_recycler_view.xml file
<?xml version="1.0" encoding="utf-8"?> <androidx.recyclerview.widget.RecyclerView |
RecyclerViewActivity.kt file
import androidx.appcompat.app.AppCompatActivity class RecyclerViewActivity : AppCompatActivity() { // Used for vertical linearlayout view // ArrayList of class RecyclerModel // This loop will create 12 Views containing // This will pass the ArrayList to our Adapter // Setting the Adapter with the recyclerview } |
card_view_layout.xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout <ImageView <TextView </LinearLayout> </androidx.cardview.widget.CardView> |
RecyclerModel.kt file
class RecyclerModel(val image : Int, val text: String) { } |
RecyclerAdapter.kt file
import android.view.LayoutInflater class RecyclerAdapter(private val dataList: List) : RecyclerView.Adapter() { override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) { // Image set to the imageview widget // Text set to the textview widget override fun getItemCount(): Int { } |
build.gradle(app) file
plugins { android { defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildTypes { dependencies { implementation 'androidx.core:core-ktx:1.7.0' |
Conclusion: We have covered how to add RecyclerView in Android using kotlin language.
Article Contributed By :
|
|
|
|
1449 Views |