Create Swipe Views With Tabs Using ViewPager
Create Swipe Views With Tabs Using ViewPager
Swipe views allow you to navigate between sibling screens, such as tabs, with a horizontal finger gesture, or swipe. This navigation
pattern is also referred to as horizontal paging. This topic teaches you how to create a tab layout with swipe views for switching
between tabs, along with how to show a title strip instead of tabs.
Note: For swiping views, we recommend the improved ViewPager2 library. For more information, see Create swipe views with tabs
using ViewPager2 and the ViewPager2 migration guide.
To set up your layout with ViewPager, add the <ViewPager> element to your XML layout. For example, if each page in the swipe
view should consume the entire layout, then your layout should look like this:
<androidx.viewpager.widget.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
To insert child views that represent each page, you need to hook this layout to a PagerAdapter. You can choose between two
kinds of built-in adapters:
FragmentPagerAdapter - Use this when navigating between a fixed, small number of sibling screens.
FragmentStatePagerAdapter - Use this when paging across an unknown number of
pages. FragmentStatePagerAdapter optimizes memory usage by destroying fragments as the user navigates away.
As an example, here's how you might use FragmentStatePagerAdapter to swipe across a collection of Fragment objects:
KOTLINJAVA
class CollectionDemoFragment : Fragment() {
// When requested, this adapter returns a DemoObjectFragment,
// representing an object in the collection.
private lateinit var demoCollectionPagerAdapter: DemoCollectionPagerAdapter
private lateinit var viewPager: ViewPager
The following sections show how you can add tabs to help facilitate navigation between pages.
To include a TabLayout in a ViewPager, add a <TabLayout> element inside of the <ViewPager> element, as shown below:
<androidx.viewpager.widget.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.viewpager.widget.ViewPager>
Next, use setupWithViewPager() to link the TabLayout to the ViewPager. The individual tabs in the TabLayout are automatically
populated with the page titles from the PagerAdapter:
KOTLINJAVA
class CollectionDemoFragment : Fragment() {
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val tabLayout = view.findViewById(R.id.tab_layout)
tabLayout.setupWithViewPager(viewPager)
}
...
}
class DemoCollectionPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {