# Hands on with ViewPager2

DevFeed: [Hands on with ViewPager2](<https://devfeed.tech/articles/hands-on-with-viewpager2-25107.md>)

Original publisher: [Read original article](<http://michaelevans.org/blog/2019/02/07/hands-on-with-viewpager2/>)

Author: Michael Evans

Published: 2019-02-08T02:38:07Z

Content type: tutorial

Language: en

Sources: [Gadget Habit](<https://devfeed.tech/sources/gadget-habit.md>)

Topics: [Library](<https://devfeed.tech/topics/library.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [build](<https://devfeed.tech/tags/build.md>), [code](<https://devfeed.tech/tags/code.md>), [components](<https://devfeed.tech/tags/components.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [google](<https://devfeed.tech/tags/google.md>), [layout](<https://devfeed.tech/tags/layout.md>), [library](<https://devfeed.tech/tags/library.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [project](<https://devfeed.tech/tags/project.md>)

## AI overview

A hands-on introduction to Google's alpha release of ViewPager2, explaining its RecyclerView-based setup, adapter implementation, dependency configuration, and vertical scrolling support.

## Source excerpt

Today Google released their alpha of ViewPager2, a signal of the nail in the coffin for the original ViewPager, originally released in 2011! Since then, I think it's safe to say that most developers have needed to make a ViewPager. Despite how prolific it is, it certainly isn't the most straightforward widget to include. I think we all have at least once wondered whether we should use a FragmentPagerAdapter or a FragmentStatePagerAdapter. Or wondered if we can use a ViewPager without Fragments. And API confusion aside, we've still had long standing, feature requests. RTL support? Vertical orientation? There are numerous open source solutions for these, but nothing official from the support library (now AndroidX)...until now! Let's dive in and try to set up ViewPager2! You'll need your project configured with AndroidX already, as well as supporting minSdkVersion 14 or higher. The first thing we'll need to do is add the library to our build.gradle dependencies. 1 implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01' If you're familiar with RecyclerView, setting up ViewPager2 will be very familiar. We start off by creating an adapter: 1 2 3 4 5 6 7 8 9 10 11 class CheesePagerAdapter(private val cheeseStrings: Array<String>) : RecyclerView.Adapter<CheeseViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheeseViewHolder { return CheeseViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.cheese_list_item, parent, false)) } override fun onBindViewHolder(holder: CheeseViewHolder, position: Int) { holder.cheeseName.text = cheeseStrings[position] } override fun getItemCount() = cheeseStrings.size } and pair it with a RecyclerView.ViewHolder. 1 2 3 4 class CheeseViewHolder(view: View) : RecyclerView.ViewHolder(view) { val cheeseName: TextView = view.findViewById(R.id.cheese_name) } Finally, just like RecyclerView, we set the adapter of our ViewPager2 to be an instance of the RecyclerView adapter. However, you'll note that there