Questions tagged [android-viewpager2]

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.

New features

  • Comparing to its predecessor android.support.v4.view.ViewPager (VP1):
  • Right-to-left (RTL) layout support
  • Vertical orientation support
  • notifyDataSetChanged fully functional (VP1 bugs addressed)

API changes

  • FragmentStateAdapter replaces FragmentStatePagerAdapter
  • RecyclerView.Adapter replaces PagerAdapter
  • registerOnPageChangeCallback replaces addPageChangeListener

Known issues

  • clipToPadding
  • no fakeDrag
  • JavaDoc
  • nested scrolling parallel to orientation
  • no offscreen limit control
  • needs better TabLayout integration
  • no pageWidth setter (forced 100%/100%)
  • page transformer: no hardware/software layer choice; no reverse drawing order
  • keep current item visible when inserting a page before current
  • keyboard navigation needs work
  • FragmentStateAdapter stability / performance improvements coming

Useful links:

Official documentation

Official sample

Fundamental Stack Overflow answer

740 questions
6
votes
2 answers

android MapView + ViewPager2 + DataBinding issue

I've recently came upon a really strange issue. I had a mapview in my layout which was inflated using databinding. After I added ViewPager2 to my layout I got this issue: java.lang.IllegalArgumentException: Wrong state class, expecting View State…
SMGhost
  • 3,867
  • 6
  • 38
  • 68
6
votes
0 answers

ViewPager2 - Is there an alternative to PagerTitleStrip?

Is it possible to create something similar to the PagerTitleStrip that existed in ViewPager? My intention is to just display titles for the fragments that are added without having a tabLayout.
6
votes
4 answers

How to disable swiping in specific direction in ViewPager2

I want to disable right to left swipe in ViewPager2. I basically have a viewpager2 element with 2 pages in my navigation drawer. I want my second page to show up only when I click some element in my first page (right to left swipe from the first…
Shahbaz Hussain
  • 542
  • 4
  • 21
5
votes
0 answers

How to set custom tag for viewpager2 fragments?

How to set custom tag for viewpager2 fragments? How can i set a tag for viewpager fragments? The above question provides answers for ViewPager. But how can this be done for ViewPager2?
5
votes
2 answers

Disable view pager swipe for other child fragments

I am working with ViewPager2 where i have 2 fragments in the viewpager. It works perfectly but there is a problem that i want it to just be swiped from FragmentTwo to FragmentOne fragment as it is working now. But when i swipe it from FragmentOne to…
5
votes
3 answers

ViewPager2 + Accessibility + talkback issue while trying to change focus and scroll with swipe gesture

I have been trying to implement ViewPager2 with accessibility with talk-back but focus is not moving beyond the second partial visible item. I want to traverse and scroll the viewpager2 with talkback and accessibility but was not able to achieve…
5
votes
0 answers

How to slow down transition animation in ViewPager2 when changing pages programmatically

I have a custom flip transformation in a ViewPager2, I've disabled user input so it can only change pages programmatically, however, the speed of the transition is way too fast. The behavior I need is with 2 fragments, the first one loads and after…
Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46
5
votes
2 answers

Fragment lifecylce behaviour using ViewPager2 and FragmentStateAdapter

I am displaying 5 fragments using ViewPager2 and FragmentStateAdapter. Each fragment has ExoPlayer instance. I am maintaining ViewPager2.setOffscreenPageLimit(1) and it is working well for loading fragments. However, when I scroll down the…
5
votes
1 answer

How to make ViewPager2 look like an infinite loop with a 'slide' page transformer?

I am looking for an infinite loop to display pictures in a ViewPager2. With a 'slide' page transformer (like default I think) between pictures, and also between last and first item, and first and last item. The difficulty I found is: to not have…
5
votes
1 answer

How do I dynamically add and remove fragment pages using Viewpager2 and Mediator Tab Layout

I need to add fragments to viewpager2 as well as tabs at runtime depending on server config file. Most viewpager2 resources I have seen showcase a static viewpager2 using the new TabMediator Tab layout. I am skipping viewpager as I need to add RTL…
5
votes
1 answer

How to get Fragment by tag in ViewPager2

I have been told to use the createFragment override of the FragmentStateAdapter to get the current fragment by index. This is how my adapter appears to be . class AddRestaurantPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : …
Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59
5
votes
1 answer

I cannot call the instantiateItem element in FragmentStateAdapter for viewPager2

When I switched to viewPager2 I had to implement another type of adapter (I used FragmentPagerAdapter and now I use FragmentStateAdapter), now I want to call the instantiateItem element that had the old adapter but it doesn't appear in the new…
5
votes
2 answers

Horizontal Scroll Recyclerview in Vertical Recyclerview with CollapsingToolbarLayout

I am building an application that uses a ViewPager2 to scroll horizontally across pages that each have a vertically scrolling content RecyclerView where the first position of each list has a row with a Horizontally scrolling widget such as another…
5
votes
2 answers

FragmentStateAdapter throws java.lang.IllegalStateException: Design assumption violated after configuration change

FragmentStateAdapter throws java.lang.IllegalStateException: Design assumption violated, when the device is rotated. How do I resolve this issue ? private inner class PagerAdapter : FragmentStateAdapter(this) { private val ids =…
4
votes
1 answer

Glide doesn't show image in first fragment "automatically"

In my application I can create albums with photos, store them on Firebase and view them. Well, in order to browse these albums I decided to create something like viewing screenshots of an app/game in the Play Store in full screen. I created Activity…