Use ViewPager2 in Android

Tram Ho

The image above is the screen on the Google Docs mobile application. Here, by swiping right / left, four times, you’ll get an idea of ​​what the Google Docs app does.

This type of View is very common in almost any application you download from Google Play. You just swipe left or right and you will get an overview of what that application does, what functions it has.

Well, the above view is ViewPager , which is very familiar. In this article, I will introduce you to ViewPager2 .

It sounds like an upgrade, such as RecyclerView and ListView. Let’s find out.

ViewPager2

ViewPager2 was released by Google on February 7, 2019, an updated version of ViewPager. It does the same thing as ViewPager but in a smarter and more efficient way.

ViewPager2 comes with a host of new features. But the most important feature is the use of RecyclerView.

Why say this is the most important feature because with ViewPager, things become more difficult when we need to add some dynamic fragments. Now, if we are using RecyclerView in ViewPager2, half of your task has been reduced.

Dig deep into ViewPager2

Like the View Pager, ViewPager2 extends from the ViewGroup class – from there, things are a bit different.

LayoutManager

LayoutManager in ViewPager2 is the same as in ViewPager. LayoutManager’s task is to determine the orientation of the components on the screen.

In Viewpager (older versions) we only had horizontal orientation, but with ViewPager2, we could set the orientation of the screen to be vertical also by calling setOrientation (). That way, we can change the screen by swiping up and down.

RecyclerView

ViewPager2 uses RecyclerView to display content on the screen. There are many benefits of using RecyclerView.

You have the ability to scroll smoothly, the data can be flexibly changed, the views are changed flexibly and smoothly. So you can use all components of RecyclerView and apply it in ViewPager2.

For example, to handle vertical orientation:

Page Change Callback

The callback when the page changes as follows:

– onPageScrolled () – will be triggered, whenever there is scrolling event for the current page

– onPageSelected () – will be activated when you select a new page

– onPageScrollStateChanged () – will be triggered when the scrolling status will be changed.

Coding

  • We will code a simple app using ViewPager2.

You open Android Studio. Choose File> New project

  • Add the following dependency to app / build.gradle :

  • To use ViewPager2, we will add the MainActivity layout as follows:

activity_main.xml

  • From here, the processing will be the same as you would with RecyclerView. We will create a new layout, which will display like the main page:

res / layout> item_page.xml

Thus, we have used 2 TextView and 1 ImageView to display random values.

OK, create layout for the finished item. Now let’s create the adapter, just like RecyclerView.

Name the class ViewPageAdapter.kt :


Finally, we call the adapter from MainActivity as follows:
Share the news now

Source : Viblo