The New Features in Android ViewPager2
By: Kayvan Kaseb
Tags: Android App Development, Viewpager2, Android Apps, Android

The picture is provided by Unsplash

Basically, ViewPager has always been a vital component of the UI in Android app development. However, it has always been a problematic issue for Android developers when they try to implement certain features such as updating Fragment content, swiping right to left, and swiping vertical. As a matter of fact, ViewPager2 is an enhanced version of the ViewPager library that provides improved functionality, and addresses some controversial issues with using ViewPager. This essay will discuss some advanced features and improvements in Android ViewPager2, which has been released by Google in recent months.

Introduction and Overview

As you know, the ViewPager is the widget that allows the user to swipe left or right to see an entirely new screen. Basically, it is an efficient way to show the user multiple tabs. In addition, it has the ability to dynamically add and remove pages or tabs at anytime. In other words, view paging is basically a layout manager, which allows the user to flip through pages of data. In short, we can be able to swipe right and left through a list of cards. ViewPager2 is an enhanced version of the ViewPager library that provides improved functionality, and addresses some difficulties with using ViewPager. In fact, ViewPager2 comes with a variety of new features and improvements. The main reason to migrate is that ViewPager2 is receiving active development support and ViewPager is not; however, ViewPager2 also offers several other specific benefits that this essay will consider these advantages.

Reasons and advantages of migrating to ViewPager2

As a matter of fact, ViewPager2 offers vertical paging in addition to traditional horizontal paging. So, to enable this feature, you should set the orientation attribute to vertical. Also, you can be able to set this attribute programmatically using the setOrientation() method. For example:

<androidx.viewpager2.widget.ViewPager2

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/pager"

android:orientation="vertical" />

ViewPager2 also has built-in right-to-left support(RTL). So, to enable this feature, you should set the app layout direction attribute to RTL.

android:layoutDirection="rtl"

At the moment, you can be able to modify your fragment collections. Also, ViewPager2 will pick up on those changes and display the fragments appropriately. In other words, your Android app can dynamically modify the fragment collection at runtime, and ViewPager2 will correctly show the modified collection.

Initially, ViewPager2 is based on RecyclerView. So you can to use DiffUtil, and you obtain the animation that DiffUtil gives you for free. Eventually, the most significant point is that all advanced development for view paging happens just only on ViewPager2. This means that ViewPager1 is no longer being actively maintained and supported.

Some steps for migrating from ViewPager to ViewPager2

In ViewPager1, you might be extending PagerAdapter. Instead, in ViewPager2, you will extend the RecyclerView’s adapter as follows:

//ViewPager

class SampleAdapter : PagerAdapter() //ViewPager2

class SampleAdapter : RecyclerView.Adapter<…>()

Similarly, instead of using getCount in ViewPager1, you will use getItemCount in ViewPager2.

//ViewPager

override fun getCount(): Int {…} //ViewPager2

override fun getItemCount(): Int {…}

Besides, if you are using ViewPager with fragments, you might be extending a FragmentStatePagerAdapter. Currently, in ViewPager2, you will extend FragmentStateAdapter. For instance:

//ViewPager

class SampleFragmentAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm,…)

//ViewPager2

class SampleFragmentAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa)

As you noticed from above example, FragmentActivity is recommended parameter to pass, but you can have some other parameters for adding. For example, you can also pass in a fragment. Or if you like, you can pass in the FragmentManager, like the original call, in a lifecycle.

//ViewPager2

class SampleFragmentAdapter(fm: FragmentManager, l: Lifecycle) : FragmentStateAdapter(fa)

Finally, instead of using getItem from viewPager1, you will now use createFragment as follows:

//ViewPager2

override fun createFragment(position: Int): Fragment{…}

This means you should create a new fragment for the specific position.

Using TabLayout

ViewPager2 introduces changes to TabLayout integration. If you currently use a ViewPager with a TabLayout object to show horizontal tabs for navigation, you will require to refactor the TabLayout object for having integration with ViewPager2. Basically, TabLayout has been decoupled from ViewPager2, and is now available as part of Material components. First, you will need to add appropriate material dependency in your build.gradle file:

implementation "com.google.android.material:material:1.1.0-beta01"

Then, you should change the TabLayout element's location in the hierarchy of your XML layout file. By using ViewPager2, the TabLayout element is indicated directly above the ViewPager2 element on the same level instead of being child of the ViewPager element.

Additionally, instead of using setupWithViewPager as in ViewPager1, you will need to use tabLayoutMediator, passing in the tabLayout and the viewPager and then attaching them. Also, inside tabLayoutMediator, you can modify the tabs. For example, here we update the tab’s text to the item’s position. For instance:

TabLayoutMediator(tabLayout, viewPager) { tab, position ->

tab.text = "OBJECT ${(position + 1)}"

}.attach()

And inside tabLayoutMediator, you can modify the tabs. For example, here we update the tab’s text to the item’s position.

In conclusion

Fundamentally, the ViewPager is the widget that allows the user to swipe left or right to see an entirely new screen. In fact, it is an efficient way to display the user multiple tabs. Furthermore, it has the ability to dynamically add and remove pages or tabs at anytime. ViewPager2 is an enhanced version of the ViewPager library that provides improved functionality, and addresses some difficulties with using ViewPager. This essay discussed some new improvements and features in ViewPager2 that has been released by Google in recent months.