[MUSIC PLAYING]
TIEM SONG: Good morning, everyone.
My name is Tiem Song.
I'm a Developer Programs Engineer on the Android DevRel
team.
And today we'll talk about ViewPager2.
Now, a quick overview of view paging.
So view paging is basically a layout manager
that allows the user to flip through pages of data.
Note here that we're swiping right and left
through a list of cards.
So if you're new to view paging or want to add it
to your app, the TLDR--
use ViewPager2, which is now available in 1.0 release
candidate.
But if you're currently using a ViewPager with your app
and want to migrate to ViewPager2,
here are some good reasons to migrate.
ViewPager2 gives you vertical scrolling.
Now you'll see here the same list of cards,
but now we're scrolling up and down through the list.
And to enable this, set the orientation
attribute to vertical.
ViewPager2 also has built-in right-to-left support.
Now, you'll note here on the tabs,
they're now listed in ascending order from right to left.
And to enable this, set the app layout
direction attribute to RTL.
Now, here are some other improvements
that ViewPager2 gives you.
You're now able to modify your fragment collections
and ViewPager2 will pick up on those changes
and display the fragments appropriately.
ViewPager2 is actually based on recycler view,
so you're able to use DiffUtil, and you get the animation
that DiffUtil gives you for free.
And this is the most important point.
All new development for view paging
happens only on ViewPager2.
ViewPager1 is no longer being actively maintained.
So with that in mind, here's how to migrate from ViewPager
to ViewPager2.
In ViewPager1, you might be extending PagerAdapter.
So in ViewPager2, you'll want to instead extend
the recycler view's adapter.
Similarly, in ViewPager1, instead of using getCount,
in ViewPager2, we'll want to use getItemCount.
If you're using ViewPager with fragments,
you might be extending a FragmentStatePagerAdapter.
Now, in ViewPager2, we actually want
to extend FragmentStateAdapter.
And you'll notice here that we're
passing in our FragmentActivity, which
is our recommended parameter to pass,
but we have a couple of alternatives.
You can also pass in a fragment.
Or if you like, you can pass in the FragmentManager,
like the original call, in a Lifecycle.
Instead of using getItem from ViewPager1,
you'll now use createFragment.
And yes, we want you to create a new fragment
for the specific position.
Now, if you're using TabLayout with ViewPager,
here are some important things to know.
Now, here's a list of tabs on top of your ViewPager.
In ViewPager2, TabLayout is decoupled
and is now available in the Material Components library.
So let's see how we use TabLayout with ViewPager2.
First, you'll need to bring in your material dependency
in your build.gradle file.
And instead of using setupWithViewPaper,
as in ViewPager1, you'll now need to use tabLayoutMediator,
passing in the tabLayout and the viewPager
and then attaching it.
And inside tabLayoutMediator, you can modify the tabs.
For example, here we update the tab's text
to the item's position.
Now, to learn more about ViewPager2,
here are a list of resources available.
We have our developer documentation
on developer.androi d.com/viewpager2.
Here you can look at reference documents, a migration guide,
and common scenarios on how to use ViewPager, such as swiping
through a list of pages.
We also have a sample available.
That's the card sample that we've shown earlier.
It's available at goo.gle/viewpager2-sample.
And it also illustrates some more advanced view cases
with ViewPager2.
And thank you for coming to my talk.
And if you're attending the conference in person,
please stop by our JetPack sandbox
for any questions about view paging.
Thank you.
[APPLAUSE]
[MUSIC PLAYING]