‫Welcome back.
‫In this video we'll continue with my adapter class and we'll talk about the three methods.
‫Three crucial methods are responsible for creating and managing the views within a list or grid Oncreate
‫view holder on.
‫Bind a view holder and get item count.
‫These methods are typically implemented within the adapter associated with the recycler view.
‫Let's start with the Oncreate view holder.
‫And by the way, I talked about these methods in deep details in the previous videos and in the Recycler
‫View section.
‫To get more details, please refer to previous sections and the previous lessons.
‫The purpose of this method is to create a new view holder for a list item.
‫It initializes the view holder and inflates the item layout.
‫This method took two parameters the first one of type view group and the second is of type integer parent.
‫The parent view group into which the new view will be attached after it's bound to an adapter position.
‫The view type is an integer that represents the type of the view to create.
‫This is useful when we have multiple views types, and we need to display multiple view types within
‫the recycler view.
‫I'll start with item card binding.
‫This is what we've done before.
‫Item card.
‫And since we added the layout so I can use it in the data binding objects binding equals to.
‫I want from you to focus with me now data binding util dot inflate data binding util dot inflate method
‫is used to inflate layout XML files just like the standard layout inflator dot inflate method.
‫You provide the layout inflator, the layout resource ID you want to inflate, and the parent view if
‫applicable.
‫So here I need to start with layout Inflator and we need to pass four parameters inflator dot from parent
‫dot, get context or dot layout, dot item card the parent and false.
‫As I told you, item card binding is a class that automatically generated by the data binding library
‫based on the name of your layout item card, and we added the binding keyword to the end of this name.
‫This is the layout inflator that is responsible for displaying the Xmls.
‫This is the layout that we need to inflate and to display for every item in the recycler view, this
‫is the parent which refers to the view group and attach to parent equals to false.
‫This is because we need to use the recycler view, and this will take the responsibility for attaching
‫to the parent by the recycler view and not this bind.
‫Return new.
‫And here we need to specify the view holder and pass the binding object to it.
‫This is the Oncreate view holder.
‫Let's move to the on bind view holder.
‫This method is called by the recycler view to bind data to an existing view holder.
‫It populates the views in the view holder with data from the data set.
‫We have two parameters the holder, the view holder to be updated with data, and the position.
‫The position of the item in the data set that corresponds to the current view holder.
‫The responsibility of this method is to retrieve the data from the item at the specified position from
‫the data set, update the views within the view holder with the data, and handle user interactions
‫or any other logic related to the item views.
‫And since we are using data binding with Recycler View, it will be an easy step here.
‫To create a user current user I specify it equals to users array list dot get.
‫And here we need to pass the position parameter.
‫Then I need to use holder dot item card binding dot set user equals to the current user.
‫You can see I am linking data binding with the current user.
‫Get item count.
‫This method returns the total number of items in the data set that the recycler view should display
‫it.
‫Calculate and return the size of the data set.
‫So here return user array list dot size.
‫Okay, it's very simple.
‫We finished the adapter.
‫Now let's move to the main activity.
‫I'll initialize the data binding objects and the adapters.
‫And here I didn't add the data binding for our main activity.
‫So let's go to here and add the layout tag with the namespaces and remove them from here.
‫And I'll add the closing tag for the layout okay.
‫Now I can access activity main binding.
‫And let's initialize these variables and objects here.
‫Data binding dot set.
‫Content view this ordered layout dot activity main.
‫And here I'll initialize the recycler view with data binding.
‫So recycler view equals to binding dot recycler view users adapter equals to new my adapter.
‫And here I need to pass this and the users array list recycler view dot set adapter and recycler view
‫dot set layout manager new linear layout manager.
‫This last step is to notify an adapter associated with the recycler view that the underlying data has
‫changed.
‫It's very simple by using user adapter dot notify data set changed.