
We get access to our users recyclerview list and we create an adapter with hard coded user information.MainActivity implements OnItemClickListener interface. We just have to implement the interface in our MainActivity. class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) As usual, our view holder has a constructor with an itemView as parameter and we get a reference to our views from item_user layout . The item_user has two textviews which holds the name and phone. Next we create our item_user.xml layout as follows.Create a new Kotlin file called RecyclerAdapter.kt.Pay attention here because it is the most important part of what this article addresses. Next, we create our view holder and adapter.

Just the above line of code gives us access to a setter and getter under the hood and other methods like toString. Data model data class User( var username:String, var phone:String) Next, we create the model class containing username and phone.


The assumption is that you have worked with the recyclerview(in Java) before and know how to create a recyclerview adapter. Add recyclerview and cardview dependencies in app level adle file as shown below.Īdd recyclerview in activity_main.xml where you removed the textview as shown below.Delete the default ‘Hello World’ TextView in activity_main.xml.Do not bother much if you do not know Kotlin. What we want is that when an item is clicked, we get the item’s model(User) information and may be pass it to a second activity.įrom Android Studio, create an empty android project(Select the Kotlin support option) and name your activity, MainActivity.There is a Recyclerview adapter with a Recyclerview with a list of items(Users in this case).In this article, I will show you how to do it in a proper way using an example scenario.

Some Android developers wonder why Google created a view like Recyclerview without a click listener(given the fact that the deprecated ListView has an item click listener).īecause there is no standard way of setting a click listener, new developers tend to confuse on the right way of doing it.
