‫You need an adapter to provide the viewpager with the content it displays.
‫For that, I'll create a new Java class and I'll name it as my Viewpager adapter For fragments.
‫You typically use fragment pager adapter or fragment state pager adapter classes.
‫These adapters manage the lifecycle of the fragments as the user navigates through them.
‫For Viewpager library version number one fragment based adapter classes inheriting from fragment, pager
‫adapter or fragment state pager adapter.
‫So if I type extend, you can see fragment pager adapter and the fragment state adapter.
‫These are deprecated because I'm not using the Viewpager library version number one, I'm using Viewpager
‫two.
‫It's better and enhanced version of Viewpager.
‫For that I'll use fragment state adapter and you can see it's imported from the Viewpager two package.
‫I need to implement the methods, create fragment and get item count method.
‫Then I need to create constructor matching the parent and I'll use this fragment manager.
‫I have these two methods get item count and create fragment.
‫Let's start with the create fragment method.
‫This method is responsible for creating and returning a fragment for a specific position within the
‫Viewpager two.
‫The purpose of this method is to create a new instance of a fragment based on the position parameter.
‫You can see this is the position parameter for each position or page In the Viewpager two you define
‫which fragment should be displayed.
‫You typically return a new instance of the appropriate fragment class.
‫For example, if you have a list of fragment classes like page fragment, you might return a new instance
‫of page fragment for each position.
‫Now I want from you to focus with me because I'm going to teach you a very important concept when using
‫the Viewpager two.
‫You need to create a list of fragments and deal with these methods the create fragment and get item
‫count and make a bridge between these two methods and the array list of fragments.
‫So for that I'll create here a private array list of fragments and I'll name it as fragment list equal
‫Alt+.
‫Enter to import the array list from the Java.util package New array list.
‫Why I'm creating this array list.
‫Since I created three fragments, I need to add them to the list and when using the create fragment,
‫I need to get the correct position of the fragment from the list and display it and the get item count
‫to return the fragment list size as the returning parameter and returning type of this method.
‫So I'll use this array list later on in the main activity to add the fragments as elements of this list.
‫Then I need to return fragment list dot get and here I need to pass the position parameter that it is
‫passed for the create fragment method.
‫Again, the create Fragment method is responsible for creating and returning a fragment for a specific
‫position within the Viewpager two and getting the specific position is from the fragment list which
‫contains a list of fragments.
‫So the returning type of this should be a fragment get item count method.
‫This method is responsible for returning the total number of elements or items or fragments that will
‫be displayed in the Viewpager two You can return three.
‫But I don't prefer this way.
‫I need to use the fragment list dot size and here I return the size of the fragment list as the elements
‫that should I should display in the Viewpager two.
‫I'll create a new method that it is not overridden.
‫I'll name it as add fragment.
‫So public void add fragment and I'll pass a fragment parameter.
‫This method and I'll use the fragment list dot add method and add this fragment in order to allow us
‫in the main activity to add the three fragments that I create.
‫So every time you create a new fragment, you should add this fragment to the fragment list, then allowing
‫this my viewpager adapter to display it and return a fragment for a specific.
‫Position within the viewpager using the fragment list dot get position.