Populating a ViewPager using a PagerAdapter

Populating a ViewPager using a PagerAdapter

What is ViewPager?

ViewPager is a Layout Manager that allows the user to move through pages of data with movements to the left or right, while when changing pages, a built-in animation is executed. Most often, those pages with data are fragments, although they can be something else, for example. images that slide…
ViewPager is filled with data using its so-called adapter. PagerAdapter. If we use fragments, specialized adapters are used for them:

  • FragmentPagerAdapter (caches fragments and is used for a smaller number of fragments, usually in conjunction with tabs)
  • FragmentStatePagerAdapter (used with more fragments)

ViewPager is inserted into the layout as a container in which all pages will be displayed.

TabLayout

ViewPager is most often integrated into the layout together with TabLayout, which represents navigation.

tabLayout

Before inserting the TabLayout, it is necessary to add a new dependecies “design”:
implementation 'com.android.support:design:28.0.0'

Only after inserting the dependencies can we add a new widget TabLayout with TabItems:

Using PagerAdapter

The

PagerAdapter is used to populate the ViewPager container with the appropriate pages. In this article, pages will be represented by fragments, and first of all it is necessary to create three different fragments (FragmentA, FragmentB, FragmentC).

a) Creating an adapter class

When we create our custom adapter, we need to extend our class either with the PagerAdapter class or with one of the already mentioned classes: FragmentPagerAdapter or FragmentStatePagerAdapter which are used to work with fragments.

MyViewPagerAdapter.java

Since we have a small number of fragments in this example, we will use a class that caches fragments (fast but good only with a small number of fragments):

As soon as we extend a class AndroidStudio requires that a constructor be generated and two methods be implemented:

The method getItem(int position) returns the corresponding fragment depending on the position (ie an int parameter representing the position of the fragment):

The getCount() method is designed to return the total number of pages to be displayed:

b) Instantiating the adapter in the activity

Once we have created the Adapter class, we need to create its instance within the activity. A new instance is created using a constructor function that is passed an instance of frameManager:

Activity

c) Connecting the adapter and the ViewPager container

Once we target the ViewPager then we can simply bind it to the adapter using the setAdapter() method:

Activity

d) Synchronizing ViewPager and TabLayout

It is already nowViewPagerAdapter populates ViewPager with fragments, so we can slide fragments with simple “swipe” movements left or right. However, although the fragments are successfully changed, no changes are made to the TabLayout. It is necessary to connect the swipe event with the change in Tablayout and vice versa. For the interaction between these two views, listeners and so-called listener pattern (you can see more about listener pattern here).

ViewPager listener

ViewPager has an interface “ViewPager.OnPageChangeListener” whose callback methods (onPageScrollStateChanged, onPageScrolled and onPageSelected) are called when a page change occurs by scrolling in ViewPager. In order for the listener to do its job, we need to set the event listener, which is done with the addOnPageChangeListener() setter method. This method is called by an object that implements this interface, which is any ViewPager.

The object that is interested in becoming a page change listener in the ViewPager is actually a TabLayout object. For this role in android there is a specific class that implements all callback methods and thus synchronizes the TabLayout after the page change in the ViewPager named “TabLayout.TabLayoutOnPageChangeListener”.
This class accepts TabLayout as a parameter in the constructor, so we will use it to create “on the fly” an anonymous object :

TabLayout listener

TabLayout has an interface “TabLayout.OnTabSelectedListener” whose callback methods ( onTabSelected(), onTabReselected(), onTabUnselected()) are called when there is a change in the tab selection.
In order for the listener pattern to do its job, we need to set the listener for these events, and this is done with the setter method addOnTabSelectedListener(). This method is called by an object that implements this interface, which is any tabLayout.

The object that is interested in becoming a tab change listener is actually a ViewPager object. For this role in android there is a specific class that implements all callback methods and thus synchronizes the ViewPager after changing tabs called “TabLayout.OnTabSelectedListener”.
This class accepts a ViewPager as a parameter in the constructor, so we’ll use it to create “on the fly” an anonymous object :

View full code




NOTE:
For quickintegration of TabLayout and ViewPager we can use the already provided “Tabbed Activity” with built-in navigation (Actions bar Tabs with ViewPager).

Viewpager boilerplate

With this activity, most of the work comes already done, so with a few minor changes, everything can be prepared very quickly.
First of all, it is necessary to create a couple of fragment layouts (eg fragment_a.xml, fragment_b.xml, fragment_c.xml) and adjust the getItem() method:

The part we can safely delete is attached to a temporary placeholder fragment :

If we don’t need FloatingActionButton then we can delete its part of the code:

If we don’t need “options menu” then we can delete the part related to it:

You can view the entire code of the “refined” activity here.

×

Activity