‫In Android development.
‫The Viewholder pattern is used in conjunction with adapter such as base adapter or Recyclerview dot
‫adapter to optimize the performance and efficiency of displaying lists or grids of data.
‫It helps improve the scrolling performance of lists by reusing views and minimizing the need to repeatedly
‫inflate and find the views for each item.
‫So in this video, we'll talk about the Viewholder pattern and how it works within a base adapter.
‫When you create views for items in a list view, for example, using the Get View method of your adapter,
‫you generally use the find view by ID method to find the individual views within each item layout.
‫However, this process can be time consuming, especially if you have a large number of items in your
‫list.
‫For example, in this list we have four items, but what about 100 items or 1000 items?
‫So we need to write 1000 times, find a view by ID, and this is not a good approach.
‫The solution is using the Viewholder pattern.
‫The Viewholder pattern aims to address this inefficiency by recycling and reusing already inflated views.
‫Let's start learning how this pattern the viewholder pattern works.
‫First of all, we need to create a Viewholder class which is a static class inside this custom adapter
‫class.
‫So it's an inner class, inner static class within your adapter that holds references to the views within
‫an item layout.
‫The purpose of this class is to store these references.
‫Once the views are found using find view by IDs so they can be reused.
‫When you're binding data to the views.
‫Let's go back to the My list item, which is a customized layout of the items.
‫We have only one text view, so I'll declare this text view here in this class.
‫TextView Text View Alt+ Enter.
‫And here we go.
‫If you have two text views, you need to create two text views.
‫Let's go up to the get view and start creating the reference for this Viewholder class view holder.
‫I'll name it as hold.
‫So the first step is to create the references to the views within an item layout.
‫The second step is to create an instance of the view holder.
‫The third step is to start associating the data source with the views.
‫If convert the view equal equal to null.
‫And you can see this is the view convert to view when you create a new view for an item inflating it
‫from the layout XML or when you're recycling a view that has a scrolled off screen, you can check if
‫the convert the view parameter is not null.
‫If it is not null, it means you can reuse this view instead of inflating and you want.
‫So the convert to view is a recycled view that you can reuse to improve the performance of your list
‫based UI components like list views, grid views and recycler view.
‫In this case, our list view.
‫So think about a converted view parameter representing one of these recycled views.
‫When you're scrolling through a list of views that go off screen are recycled so they can be reused
‫for new items that are coming onto the screen.
‫And the converted view parameter represents one of these recycled views.
‫So if it is not null, if it is recycled, I'll use this else here.
‫I'll set Holder, which is the view Holder class and I'll initialize it with as a view holder, I'll
‫casting it and convert the view dot get tag.
‫The primary purpose of using get tag method in the get view method is to retrieve a previously associated
‫Viewholder object.
‫The view holder pattern is used to optimize the view recycling and improve the performance of list based
‫UI components.
‫And for this purpose we use the get tag in order to associate it to the holder object.
‫So when the convert view is null, you are inflating a new view and this code else if it is not null,
‫you are using the view which improves performance as you're inflating views when necessary.
‫I'll use convert view equal to layout inflator dot from context dot inflate.
‫I'll go to new line or dot layout dot mock.
‫List item.
‫The second parameter is parent, which is a viewgroup.
‫And the third parameter is false, which is attached to root.
‫But what is the layout inflator and why we use it?