‫Welcome back.
‫In this video we're going to create the adapter class that this Viewpager two will use to display fragment
‫one, fragment two, and fragment three.
‫By the way, Viewpager two is an updated version of the original Viewpager component in Android.
‫It's part of the Android Ext library and is designed to address some limitations of the original Viewpager
‫class.
‫Viewpager two is specifically created to work with RecyclerView and offers improved flexibility, performance,
‫and ease of use.
‫It's commonly used for implementing horizontal scrolling views with fragments or other views, and I'm
‫going to use it in this application to create a horizontal scrolling views with fragments.
‫Let's move to create a new Kotlin class and I'll name it as my pager adapter.
‫Viewpager two uses an adapter similar to the RecyclerView dot adapter class.
‫You can create a custom adapter to supply the content for each page.
‫I'm going to use the fragment state adapter, so here extend it from fragment state adapter class.
‫The recommended adapter for use with a Viewpager two is a fragment state adapter.
‫It's designed to work with fragments and is an improvement over the original fragment state pager adapter.
‫Then change it to constructor invocation.
‫Here I need to pass two parameters, one of type fragment manager and one of type lifecycle.
‫You can use one type of parameters which is the fragment or fragment activity, but I prefer to use
‫fragment manager and lifecycle parameters.
‫But from where I need to get these parameters, I need to get them from the primary constructor of this
‫class.
‫So this class primary constructor will get a fragment manager fragment manager FM.
‫It's of type fragment manager.
‫And the second parameter is lifecycle LC which is the life cycle parameter.
‫I'm going to pass FM and LC here inside this constructor invocation of the fragment state adapter.
‫Again guys when you create an adapter the Viewpager adapter that extends from fragment state adapter,
‫we need to pass a fragment manager and lifecycle from where we need to get them.
‫We need to get them from the primary constructor of this class.
‫The second step is to implement the members.
‫Here.
‫We need to hover on my pager adapter and click on Implement Methods, get Item Count and Create Fragment.
‫Click okay and here we go.
‫We implemented these two methods get item Count and Create Fragment.
‫The first method get item count will return the number and the total number of items, fragments or
‫views in your Viewpager two.
‫The number represents the total number of pages the view pager two should have.
‫Since I have three fragments, I can return three, but it's not preferable.
‫I can return fragment list dot size, but from where we need to create the fragment list, I'm going
‫to create it here inside my pager adapter.
‫This list will contain all the fragments in an array list.
‫So var frag array is of type array.
‫List of type fragment equals to array list.
‫I can change it to fragment list.
‫How to add members to this list I can create a new method here inside this class responsible for adding
‫fragments to the fragments list.
‫For that I can create function add fragment to list.
‫It takes into the parameter a fragment and it will execute fragment list dot.
‫Add the element fragment.
‫I'm going to use this method to add a fragment one, two, and three to this list.
‫And then from this list I'm going to get the total size.
‫So fragments list dot size this.
‫The second method here is create fragment.
‫The create fragment method is responsible for creating and returning a new instance of a fragment for
‫a given position or page.
‫In the Viewpager two I will return fragment list dot get position.
‫This position is the parameter that it is passed for the create fragment method, I'm going to get all
‫the fragments in the same order that they are in.
‫The fragments list, which is the array list of fragments.