views-widgets-samples/ViewPager2 at master · android/views-widgets-samples (original) (raw)
This sample shows how to use ViewPager2with either Views or Fragments as pages, how to perform page transformations, how to link ViewPager2 to a TabLayout, and demonstrates handling modifications of an underlying page adapter collection.
ViewPager2 is the replacement of ViewPager, addressing most of its predecessor's pain-points, including right-to-left layout support, vertical orientation and modifiable Fragment collections.
Samples
- ViewPager2 with Views - shows how to set up a ViewPager2 with Views as pages
- ViewPager2 with Fragments - shows how to set up a ViewPager2 with Fragments as pages
- ViewPager2 with a Mutable Collection (Views) - demonstrates usage of ViewPager2 with Views as pages and mutations in a page adapter
- ViewPager2 with a Mutable Collection (Fragments) - demonstrates usage of ViewPager2 with Fragments as pages, and mutations in a page adapter
- ViewPager2 with a TabLayout (Views) - shows how to set up a ViewPager2 with Views as pages, and link it to a TabLayout
Getting Started
Setting up ViewPager2
Just like ViewPager, ViewPager2 needs an adapter to populate it with pages. AnyRecyclerView.Adapter will suffice for simple use cases, when your pages do not have state that needs to be maintained across the Activity lifecycle. The top level View that you will inflate for your pages must have its layout_width and layout_height set tomatch_parent. If your pages do need to save state across lifecycles, make your adapter implement theStatefulAdapter interface, or manage your own state saving (e.g., using ViewModel).
A simple stateless example can be found inCardViewActivity.
Using Fragments as pages
If you want to use Fragments instead of simple Views for your pages, have your adapter extendFragmentStateAdapter. In it, simply return a new Fragment in itsgetItemmethod. The FragmentStateAdapter implements StatefulAdapter, so your fragments are automatically a part of the lifecycle. Implement theironSaveInstanceStateas you would do normally.
You can find an example of stateless Fragments inCardFragmentActivity, and of stateful Fragments inMutableCollectionFragmentActivity.
Working with TabLayout
With the original ViewPager, you were able to link it to a TabLayout by using TabLayout'ssetupWithViewPager. With ViewPager2, the integration comes in the form ofTabLayoutMediator. Simply create an instance of this class, pass an implementation of its OnConfigureTabCallback to the constructor, and call attach() when you've set your ViewPager2's adapter.
You can find an example of a ViewPager2 that's linked to a TabLayout inCardViewTabLayoutActivity.
Testing
Performing UI tests on a ViewPager can be done by performing swipes on the ViewPager element, or by calling setCurrentItem on the ViewPager directly. Examples can be found inViewPagerTest,MutableCollectionTest andTabLayoutTest.
If you need to wait until a swipe or page transition has finished, there are two strategies you can employ: create an IdlingResource that is idle whenever the ViewPager's scroll state is idle, or create a CountDownLatch that counts down when ViewPager2 transitions to idle. An example of an IdlingResource has been implemented inViewPagerIdleWatcher.
Some useful Espresso extensions can be found inViewInteractions andViewPagerActions.
Support
You can report issues on ViewPager2 or the samples from this repository here.


