‫Welcome back.
‫In this video we'll create the adapter class and we'll learn how to add data binding to the recycler
‫view, create a new Java class and name it as my adapter.
‫And this adapter will extend from Recycler View dot adapter.
‫And you can see here I need to pass a Viewholder.
‫We talked about view holders.
‫We talked about adapters.
‫We talked about the three methods that we need to implement on bind Viewholder get count, and on create
‫Viewholder.
‫So there are a lot of details, a lot of topics we talked about in the previous section.
‫So please guys refer to the Recycler view section and the previous sections when we use the recycler
‫view with data binding.
‫But don't worry if you are following me step by step, I'll review these concepts here.
‫I need to pass a viewholder.
‫What is a viewholder?
‫A Viewholder class is an essential component of the recycler view framework, and it plays a crucial
‫role in optimizing the performance of lists and grids in your app.
‫The purpose of the Viewholder is to cache references to the individual views within an item layout or
‫recycler, view list or grid.
‫Let's start creating this inner class.
‫Public class user Viewholder extends from recycler view dot Viewholder.
‫User Viewholder is extending from RecyclerView dot Viewholder.
‫It holds the references of the individual views, so you don't need to call find view by id method repeatedly
‫for each item in the list when scrolling, which can be resource intensive and slow.
‫So whenever you create a custom adapter for the RecyclerView, you typically create and manage a view
‫holder.
‫Instances.
‫When creating the adapter and the recycler the view holder, I need to copy it and pass it here my adapter
‫dot user view holder.
‫So here I am passing the correct data type again.
‫Guys, I prefer creating the class at first because many people have an error here when passing the
‫data type or the generics in the recycler view adapter.
‫So always start by creating the adapter.
‫Extend it from RecyclerView dot adapter, and then the next step would be creating a class A view holder
‫class that extends from recycler view dot view holder, and then pass this class as a type parameter.
‫Here in the generics of the recycler view dot adapter.
‫Inside the constructor here Alt+.
‫Enter.
‫Create the constructor.
‫You can initialize the view references by calling find view by id for each view you need to work with.
‫But since we are using the data binding and the latest updates of Android prefer using data binding,
‫I'll use it here and I want from you to focus with me because it's a very important step.
‫How to add data binding to the recycler view.
‫In the previous videos, we started here to find the view by ID for the correct view.
‫Now, since I'm using the data binding, I'll start by creating an instance of the data binding of the
‫item card.
‫So item card binding item card bind and here I need to initialize it.
‫And despite using item view and passing it as an item view I remove it and create a new constructor.
‫Remove this item.
‫And here the super.
‫I need to change it to item card binding dot get root.
‫The super keyword is used to call the constructor of the parent class, which is the recycler view dot
‫Viewholder.
‫In this case, and item binding or item card binding dot get root method.
‫Returns the root view of the item layout by passing the root view to super method.
‫You are initializing the parent Viewholder class with the root view, which is what Recycler View dot
‫Viewholder expects and to handle the click events on the Recycler View items, you can use item binding
‫dot get root.
‫You can see it's returning type is a view dot set on click listener, new view on click listener and
‫you can write the code and trigger the code when the user clicks on any of these items.
‫And since we are talking about onClick, I'll show you if you.
‫If you want to use and get the clicked item, you start by end position equals to get and after position.
‫Here you are getting the app position of the clicked item.
‫But I'll not use this because I don't need to handle the click events on the item of the recycler view.
‫Let's go back to here and start implementing the methods of the Recycler View Adapter class.
‫And here I need to start adding some variables such as the context, because since I'm using the adapter
‫and I need to initialize it here in the main activity, I need to pass the context of the main activity
‫to the adapter in order to display, for example, a toast message if I want, or any other format that
‫requires the context alt+ enter.
‫But if you want to display any toast message, you need to use this context ArrayList of user class.
‫I name it as user ArrayList Alt+.
‫Enter to import the array list.
‫I'm going to store the variables and the users inside array list collection Alt+ insert.
‫To create the constructor having the context and the array list.
‫Again, I need to implement the three methods Alt+.
‫Enter to implement the methods on create Viewholder on bind Viewholder and get item count.
‫Click okay.
‫And we talked about these three methods in deep details in the previous videos.
‫So please refer to the RecyclerView section and the the previous sections of mVVM with room and mVVM
‫with retrofit to learn more about these methods.
‫But don't worry, I'll give you an overview about these.
‫Methods.