ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is mostly found in apps like Youtube, Snapchat where the user shifts right – left to switch to a screen. Instead of using activities fragments are used. It is also used to guide the user through the app when the user launches the app for the first time.
ViewPager Using Fragments in Android
Steps for implementing viewpager:
Adding the ViewPager widget to the XML layout (usually the main_layout).
Creating an Adapter by extending the FragmentPagerAdapter or FragmentStatePagerAdapter class.
An adapter populates the pages inside the Viewpager. PagerAdapter is the base class which is extended by FragmentPagerAdapter and FragmentStatePagerAdapter. Let’s see a quick difference between the two classes.
Difference between FragmentPagerAdapter and FragmentStatePagerAdapter:
FragmentStatePagerAdapter: Keeps in memory only the current fragment displayed on the screen. This is memory efficient and should be used in applications with dynamic fragments. (where the number of fragments is not fixed.).
FragmentPagerAdapter: This adapter should be used when the number of fragments is fixed. An application that has 3 tabs which won’t change during the runtime of the application. This tutorial will be using FragmentPagerAdapter.
Following is the structure of the ViewPagerAdapter Class:
getCount(): This method returns the number of fragments to display. (Required to Override)
getItem(int pos): Returns the fragment at the pos index. (Required to override)
ViewPagerAdapter(@NonNull FragmentManager FM): (required) The ViewPager Adapter needs to have a parameterized constructor that accepts the FragmentManager instance. Which is responsible for managing the fragments. A FragmentManager manages Fragments in Android, specifically, it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.
getPageTitle(int pos): (optional) Similar to getItem() this methods returns the title of the page at index pos.
add(Fragment fragment, String title): This method is responsible for populating the fragments and fragmentTitle lists. which hold the fragments and titles respectively.
A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
The three widgets AppBarLayout used to host the TabLayout which is responsible for displaying the page titles. ViewPager layout which will house the different fragments. The below Image explains the important parameters to set to get the app working as intended.
In the TabLayout we need to add the tabmode = “fixed” parameter which tells the android system that will have a fixed number of tabs in our application. Add the following code to the “activity_main.xml” file.
onCreateView( onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState): This method is responsible for inflating (parsing) the respective XML file and return the view which is added to the ViewPager adapter.
onCreate(Bundle SaveInstanceState): This methods is similar to activities OnCreate() method.
Designing the Page XML Files. All the fragments XML layouts have the same designs. We have a TextView at the center displaying the name of the respective page, the root container used here is FrameLayout whose background is set to #0F9D58
Below is the code for the fragment_page1.xml file:
Please Login to comment...