RecyclerView is an improved version of List View in which we can add customization to each item of our recycler view according to our requirements. We can change the orientation of our recycler view depending on our requirements. We can create a simple grid layout, vertical and horizontal recycler view, and a staggered grid view. In this article, we will take a look at building a RecyclerView with a GridLayoutManager in Android using Kotlin.
Step 2: Create a Card Layout for Recycler View Card Items
Go to the app > res > layout> right-click > New >Layout Resource File and name the file as course_rv_item. In this file, all XML code related to card items in the RecyclerView is written. Below is the code for the course_rv_item.xml file.
Go to the app > java > Right-Click on your app’s package name > New > Kotlin Class and name the file as CourseRVModal. Add the below code to it. Comments are added in the code to get to know in detail.
// on below line we are creating a
// two variable one for course name
// and other for course image.
var courseName: String,
var courseImg: Int
Step 4: Create a new Kotlin class for the Adapter
Similarly, create a new Kotlin Class and name the file as CourseRVAdapter. The adapter is the main class that is responsible for RecyclerView. It holds all methods which are useful in RecyclerView. Adapter class is used to set the data to each item of our recycler view.
<!--on below line we are creating a recycler view-->
Step 6: Working with the MainActivity.kt file
This is the main java file where we will set LayoutManager, adapter, and set data to RecyclerView which is to be displayed in RecyclerView. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Please Login to comment...