‫We said before that when extending recycler view adapter, we need to implement three methods.
‫Oncreate.
‫Viewholder.
‫Unbind Viewholder and get item count.
‫Let's start with the Oncreate Viewholder.
‫It's responsible for creating new view holders for your items.
‫It's called by recycler View When it needs to create a new item view holder for a list item that will
‫be displayed on the screen.
‫And the parameters are of two types one of type view group and one of integer.
‫The first parameter is of type view group in which the new item view will be eventually placed.
‫The recycler view will determine the correct parent to pass based on its layout and structure.
‫The purpose of this parameter is to provide layout parameters to the layout inflator so the inflated
‫view can be correctly sized and positioned within its parent.
‫And this is what we've done before.
‫We use this view group parameter.
‫The second parameter is of type int called view type.
‫The view type parameter is used when your recycler view has multiple types of items.
‫This can be useful when you need different layouts or behaviors for different types of items.
‫For example, if your adapter handles different item layouts for headers, footers and regular items,
‫you can use the view type parameter to distinguish between them.
‫In most cases, you won't need to use this parameter and can set it to a constant value later on in
‫the next videos.
‫And when we deal with the types of recycler view, we'll use the view type parameter.
‫The second method is called Unbind the Viewholder.
‫It's responsible for binding data from your data set to the views within a view holder for a specific
‫item.
‫This method is called by the recycler view.
‫When an item is about to be displayed on the screen.
‫This method took two types of parameters one of viewholder and one of type int.
‫The holder parameter is an instance of your custom view holder class that we created before that holds
‫references to the views within the item layout.
‫This holder was created and returned by Oncreate Viewholder method.
‫You use this holder to access and manipulate the views within the item layout.
‫The second parameter is of type int called position.
‫The position parameter indicates the position of the item in the data set that you need to bind the
‫views.
‫This position corresponds to the position of the item in the list that the recycler view is currently
‫displaying.
‫Let's create an instance of our model class here.
‫Item item Equal item list dot get position.
‫I'm using this parameter, then I'll use the holder parameter dot.
‫I'll access the text view that I need to initialize and make references in our static class.
‫So here I'll move to my view holder and make the references for our views which are image view and two
‫text views.
‫So I need to start with the image view.
‫Image view alt plus enter to import the package of the widget and import the class of the image view
‫and two text views.
‫Alt+ enter import the package of the text view and here under the super in the constructor of my viewholder,
‫I'll set image view equal to item view dot find view by id r.id dot image view text view or title.
‫Equal item view.
‫And the Viewholder is a class that holds references to the views within the item layout.
‫It's responsible for improving the performance by avoiding redundant find view by ID methods, calls
‫and efficiently accessing and updating the views.
‫And these are the member variables that store references to image, view and text views within the item
‫layout.
‫By storing references to these views, you avoid the need to call find view by ID every time you need
‫to access or update them.
‫And this is the constructor, the super and I'm passing the item view.
‫This line calls the constructor of the super class recycler view dot viewholder and passes the item
‫view parameter to it.
‫This initialize the Viewholder with the root view of the item layout and this line of codes.
‫Use the find view by ID method to find and assign image, view and text views within the item view.
‫By doing this, the in the constructor you're performing the view lookups only once when the view holder
‫is created and not every time the item is bound in the on bind Viewholder method.
‫By creating a custom view holder that holds references to the views within the item layout, you are
‫optimizing the performance of your recycler view.
‫The view holder acts as a cache for the views allowing you to efficiently access and manipulate the
‫views during the binding process.
‫This separation of concerns helps keep your code responsive, especially when dealing with large data
‫sets.