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
12
votes
2 answers

Open ViewPager2 to particular position without scrolling

I have implemented a recyclerview with some videos (say 10). Clicking on any item opens a viewpager2 which is on some other activity with all the items and the clicked one (from recyclerview) is shown. After that user can scroll to other item from…
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
12
votes
6 answers

How to add endless/infinite scroll to ViewPager2?

I have a FragmentStateAdapter that is attached to new ViewPager2. In the previous ViewPager implementation there were number of ways to implement endless/infinite scroll. How to do so in ViewPager2? Thanks in advance.
Ali Yucel Akgul
  • 1,101
  • 8
  • 27
  • 53
12
votes
3 answers

Why is the Android ViewPager2 class final?

While using ViewPager2 in my current project, I wanted to use setOnTouchListener, but I was unable to get any events. I decided to extend the ViewPager2 class to override the touch event methods, but was unable to as the class definition is…
Ahmed Elshaer
  • 364
  • 7
  • 21
11
votes
1 answer

ViewPager2 inside a fragment leaks after replacing the fragment it's in by Navigation Component navigate

At first, i had the issue for ViewPager2 inside a tab of BottomNavigationView and data binding , data binding also leaks with ViewPager2 and should be nulled in onDestroyView, leaking and managed to narrow the issue to ViewPager2 while navigating…
11
votes
2 answers

BottomSheetBehavior with ViewPager2 can't be scrolled down by nested RecyclerView scroll

I have a view that acts like BottomSheetBehavior and this view has ViewPager2 inside. Each ViewPager2's page is a vertical RecyclerView. The issue is that BottomSheet doesn't scroll down when current vertical RecyclerView (which is a page of…
SimpleAndroid
  • 281
  • 2
  • 8
11
votes
3 answers

Prevent Swiping of ViewPager2 when onTouch of inner view

I have a ViewPager2 with a fragment inside, in the fragment, I have a custom view with certain touch logic that involves moving the finger. how do I prevent the ViewPager from swiping while the inner view intercepts the touch event? override fun…
Vasili Fedotov
  • 1,091
  • 14
  • 31
10
votes
4 answers

How to make ViewPager2 less sensitive?

I'm using ViewPager2 in my App, it has three pages and each page show different contents, the issue is that it's too sensible that the ViewPager will change the pages even if someone is scrolling down and for mistake moves the finger a bit to right…
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
10
votes
1 answer

How to make ViewPager2 less sensitive to swipe?

My ViewPager2 contains fragments with RecyclerView. When I scroll down and up inside adapter, which has a few items (not cover whole screen), it very often "swype" to next or previous fragment in view pager. Is there any way to decrease sensitivity…
Michalsx
  • 3,446
  • 5
  • 33
  • 46
10
votes
2 answers

ViewPager2 not able to dynamically add remove fragment

Removing/Adding fragments at index results in unexpected behaviour in Viewpager2. This was not possible with ViewPager but expected to work with Viewpager2. It causes duplicate fragments and out of sync TabLayout. Here is a demo project which…
10
votes
3 answers

Vertical ViewPager2 with RecyclerView Scrolling Issue

I'm using a ViewPager2 with two fragments in a vertical orientation. When the user swipes down to the second fragment, there is a RecyclerView that scrolls content in the same vertical direction. The issue is that when I scroll the contents of the…
10
votes
2 answers

ViewPager2/Tabs problem with ViewModel state

I am following the MVVM pattern - meaning I have a ViewModel for each Fragment. I added two tabs by using ViewPager2. My adapter looks like this: @Override public Fragment createFragment(int position) { switch (position) { case 0: …
10
votes
0 answers

"Expected the adapter to be 'fresh' while restoring state" in Viewpager2 FragmentStateAdapter

When I try to re-use a Viewpager2 FragmentStateAdapter, I get this error. I already tried to set the adapter to null in onDestroyView but this does not help. This occurs because I am using the Jetpack Navigation libarary. When I navigate away from…
sunilson
  • 1,449
  • 15
  • 31
10
votes
1 answer

Fragment disappears with ViewPager2 and FragmentStateAdapter

I'm building an app using ViewPager2 that displays different fragments when swiped. My adapter extends FragmentStateAdapter as described here. When I put my app into the background and then resume again, the current fragment disappears. What do I…
rstockbridge
  • 233
  • 3
  • 11
9
votes
1 answer

E/iardini.error0: Invalid ID 0x00000001 - Error resource ID

I am currently stuck on an error that continues to slow down the performance of my application. The error does not cause the application to close or crash, or to write an exception to the console. The console simply reports this…
9
votes
2 answers

ViewPager2 - How to use registerOnPageChangeCallback to check view pager item position

How can I use ViewPager2's registerOnPageChangeCallback facility to check the position of the currently visible ViewPAger2 fragment? I know this will consists of an if/when statement but don't know the correct way to implememnt…
wbk727
  • 8,017
  • 12
  • 61
  • 125
1 2
3
49 50