‫Welcome back.
‫In this video, we'll create the adapter class for the recycler view since I'm going to display the
‫items into a recycler view.
‫And I have this custom layout that I created for displaying the movies, I need to create an adapter
‫that extends a recycler.
‫View that adapter class.
‫Inside this view, I'll create a new Java class and I'll name it as movie adapter.
‫This class will extend from recycler view dot adapter.
‫We've done this millions of times in the previous section, so please, if you missed any detail, any
‫concepts, please refer to the recycler view sections and the previous sections.
‫But don't worry, I'll explain everything here.
‫I need to pass a view holder data type.
‫I'll create it first a class.
‫Name it as movie view holder that extends recycler view dot Viewholder.
‫Make it public and I'll pass this movie view holder directly here in the data type generics.
‫So movie adapter dot movie viewholder you can see it.
‫This is the data type that it's passed as generics for the recycler view adapter.
‫Inside this view holder, I'll create an instance from binding object.
‫I need movie list item.
‫As we learned before in the data binding.
‫I'll start with movie with a capital letter movie list item binding and I name it as movie list item
‫binding.
‫Create a constructor having this data binding as a parameter and I'll remove this view because I'll
‫pass it here in the super movie list.
‫Binding dot get route.
‫This will return the view and here I need to set movie list item dot get route.
‫You can see I'm returning a view dot set on click listener.
‫Later on I'll implement this view on Click Listener because I need to direct to the movie details activity.
‫And for now everything is working fine.
‫We created a movie view Holder class.
‫The second step is to create variables and implement the three methods of the Recycler View adapter
‫class.
‫I'll start with private context object named context and private array list of movie class and name
‫it as movie array list Alt+.
‫Enter to import the movie class.
‫Create a constructor.
‫Having these two variables when creating a layout inflator, I need to use a context object and I need
‫to pass it to the from method as a parameter.
‫The last step is to implement the methods Alt+.
‫Enter to implement the methods and make sure when you implement the methods you are passing this Viewholder
‫data type implement the methods three methods Enter and Oncreate View holder on bind Viewholder and
‫get item count.
‫So these methods are part of a recycler view adapter class is responsible for providing data to the
‫recycler view and creating views for each item in the list.
‫Let's start with the Oncreate view Holder.
‫This method is called when the recycler view needs to create a new view holder for an item view.
‫It's responsible for inflating the layout of the item view, usually from an XML layout file and returning
‫a new instance of a custom view holder class that holds references to the views within the layout.
‫So let's start with the Oncreate view holder.
‫I'll use the movie list item binding.
‫So movie list item binding instance binding equal to data binding util dot inflate method.
‫And inside this inflate method I need to pass four parameters Again guys, everything is explained in
‫the previous sections about data binding with the recycler views data binding with text views, two
‫way data binding and many other concepts.
‫The first parameter is the layout inflator layout inflator dot from and here I need to pass the context
‫object.
‫So for that I have created this context object.
‫You can see why I'm doing this.
‫I'm teaching you why we are writing this code.
‫Also, you can use the parent view group like this parent dot Get context to return the context.
‫The second parameter is r dot layout dot movie underscore list underscore item.
‫This is the layout that we need to inflate to display.
‫The third parameter is parent, which is the view group.
‫And the fourth parameter is attached to parent, which is false.
‫Again guys data binding util dot inflate is a method used to inflate a layout to create.
‫A binding instance for it.
‫It's a part of data binding library and it's used to associate a layout XML file with the generated
‫binding class and layout inflator from parent get context method.
‫This line creates an instance from a layout inflator using the context of the parent view.
‫The layout inflator is used to inflate the layout XML file into a view object or the layout, which
‫is the layout that we need to display and refers for a single item in your model list and the parent
‫which is a parameter or parent view parameter to which the inflated layout will be attached and false.
‫This parameter indicates whether the inflated layout should be attached to the parent view immediately.
‫Setting it to false means that the layout will not be attached to the parent at this point.
‫Then I need to return new movie Viewholder and I'm passing my binding instance on bind Viewholder.
‫This method is called when the recycler view wants to bind data to the Viewholder and display it on
‫the screen.
‫It's responsible for updating the views within the Viewholder with the data from a specific position
‫in the data set.
‫The position parameter in this method corresponds to the position of the item within the data set here
‫inside this on bind.
‫Viewholder.
‫I'll start with the movie instance equal to movie array list dot get and here I need to get the current
‫movie and the holder dot movie list binding and set movie to this movie.
‫I am linking the data binding with this movie or the current movie in the data set to get item count.
‫This method determines the number of items in the data set that the recycler view should display.
‫It's called by the recycler view to know how many items there are in the data source and the get item
‫count method should return an integer representing the total number of items.
‫I'll use movie array list dot size.
‫So congratulations guys.
‫We created this movie adapter for the recycler view.