Turning the Page: Migrating to ViewPager2 (Android Dev Summit '19)
[Music] good morning everyone my name is Tim song I'm a developer programs engineer on the Android dev rel team and today we'll talk about viewpager to now cook overview of view paging so view paging is basically a layout manager tha
t allows the users to flip through pages of data you know 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 view pager – which is now available in 1.0 release candidate but if you're currently using a view pager with your app and want to migrate a view pager – here are some good reasons to migrate a view pager – gives you vertical scrolling now you'll see here the same list of cars but now we're scrolling up and down through the list and to enable this set the orientation attribute – vertical view pager – also has built in a right-to-left support now you'll know 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 – RTL now here are some other improvements that viewpager 2 gives you you're now able to modify your fragment collections and view pager – will pick up on those changes and display the fragments appropriately view pager – is actually based on recycler view so you're able to used if you tilt and you get the animation that if you till gives you for free and this is the most important point all new development for view paging happens only on view page to view pager 1 is no longer being actively maintained so with that in mind here's how to migrate from view pager to view pager – if you Peter 1 you might be extending pager adapter so undo pager – you want to instead extend the recycler views adapter similarly in view pager 1 instead of using instead using get count and viewpager to will want to use get item count if you're using viewpager with fragments you might be extending a fragment state pager adapter now I'm viewpager – we actually want to extend fragment state adapter and you'll notice here that we're passing in our framing activity 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 fragment manager like the original call and a lifecycle and instead of using get item from view pager one you will now use create fragment and yes we want you to create a new fragment for the specific position now if you're using tab layout with view pager here are some important things to note now here's a list of tabs on top of your view pager and view pager to tab layout is decoupled and is now available in the material components library so let's see how we use tab layout with view pager – first you'll need to bring in your material dependency in your build.
gradle file and instead of using setup with view pager as in view pager one you'll now need to use tab layout mediator passing in the tab layout and the view pager and then attaching it and inside tab layout mediator you can modify the tabs for example here we update the tabs text to the idn't position now to learn more about view pager – here are a list of resources available we have our developer documentation on developer.
android.
com slash view pager – here you can look at reference documents a migration guide and common scenarios on how to use view pages such as swiping through a list of pages we also have a sample available that's the car sample that we shown earlier it's available at goog alvie's slash view pager due to – sample and it also illustrates some more advanced if you cases cases of with view pager – and thank you for coming to my talk and if you're attending the conference in person please stop by our jetpacks sandbox for any questions about view paging thank you [Applause] [Music] you.
Comments
Post a Comment