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
639
votes
43 answers

Why `PagerAdapter::notifyDataSetChanged` is not updating the View?

I'm using the ViewPager from the compatibility library. I have succussfully got it displaying several views which I can page through. However, I'm having a hard time figuring out how to update the ViewPager with a new set of Views. I've tried all…
C0deAttack
  • 24,419
  • 18
  • 73
  • 81
168
votes
7 answers

How to disable swiping in ViewPager2?

Is it possible to enable-disable swiping in new android viewpager2 component?
murgupluoglu
  • 6,524
  • 4
  • 33
  • 43
149
votes
13 answers

Proper implementation of ViewPager2 in Android

I came to know about ViewPager2 and tried to implement it, but didn't find any proper example. Can anyone tell me how can I use it. I am looking for proper usage, not an example.
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
102
votes
12 answers

How to use TabLayout with ViewPager2 in Android

I want to use com.google.android.material.tabs.TabLayout component with Android's new ViewPager implementation androidx.viewpager2.widget.ViewPager2. However, the setupWithViewPager(..) method provided by TabLayout supports only the old ViewPager…
52
votes
23 answers

Pages must fill the whole ViewPager2 (use match_parent)

I have an item layout where I display an image, product name, and product image. I must display image in 1:1.5 ration using constraint layout. But when I load a small image, below texts not displaying. Below is my code of item XML:-
Mahesh Babariya
  • 685
  • 2
  • 8
  • 12
46
votes
5 answers

Viewpager2 with fragments and Jetpack navigation: Restore fragments instead of recreating them

I have a Viewpager2 inside a Fragment (lets call it HomeFragment). That Viewpager itself also contains Fragments. When I navigate away from the HomeFragment its view will be destroyed and when I navigate back the view will be recreated. Now I set…
42
votes
13 answers

ViewPager2 default position

I'm creating a slideshow with ViewPager2. For example, the slideshow has 3 items and I want to show the second item when the activity opens. I use setCurrentItem(int item, boolean smoothScroll) method but it doesn't work and nothing happens. How can…
hosseinAmini
  • 2,184
  • 2
  • 20
  • 46
37
votes
19 answers

ViewPager2 with differing item heights and WRAP_CONTENT

There are a few posts on getting ViewPager to work with varying height items that center around extending ViewPager itself to modify its onMeasure to support this. However, given that ViewPager2 is marked as a final class, extending it isn't…
Meetarp
  • 2,331
  • 1
  • 23
  • 31
33
votes
2 answers

remove fragment in viewPager2 use FragmentStateAdapter, but still display

I have a viewPager2 and FragmentStateAdapter, and there are Fragement1, 2,3 4, I am in fragment2, and want to remove fragment3, and display fragment4 after fragment2. The problem is it always show me fragment3(data), the debug shows the fragment3…
27
votes
4 answers

Change ViewPager2 Scroll Speed when sliding programmatically

I know there is way to change animation duration of ViewPager programmatical slide (here). But its not working on ViewPager2 I tried this: try { final Field scrollerField = ViewPager2.class.getDeclaredField("mScroller"); …
Saeed Arianmanesh
  • 1,269
  • 2
  • 22
  • 33
27
votes
5 answers

Android ViewPager2 setPageMargin unresolved

I want to implement Carousel using View Pager2 with preview of left and right page like this: Initially I was using view pager1 which supported. Now I think it's removed viewPagerhost.setPageMargin(20); Any idea how we can achieve this using…
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
23
votes
5 answers

How to fix 'Design assumption violated' error in ViewPager2?

I'm using ViewPager2, Kotlin and AndroidX. When the adapter is not at index 0 and I change the adapter list and set current item to index 0 the exception is thrown. java.lang.IllegalStateException: Design assumption violated. at…
Amir
  • 1,628
  • 20
  • 28
22
votes
6 answers

Fragment no longer exists for key FragmentStateAdapter with Viewpager2

I am using ViewPager2 for my project. I need to use nested fragments inside a fragment with viewpager2. it works like charm until I try to navigate between fragments(not nested ones). After the first time navigating between fragments, the…
22
votes
7 answers

Remove ViewPager2 Overscroll animation

Can't find a way to remove the ViewPager2 overscroll shadow animation. I know on ViewPager, you can directly just set the overscrollMode attribute to never, however, it does not work on ViewPager2 Already tried the…
Daniel Kim
  • 899
  • 7
  • 11
19
votes
0 answers

Android: How to intercept touch event in ViewPager2

How to prevent swiping in androidx ViewPager2 when the touch event is coming from inside a nested view (e.g. a HorizontalScrollView, etc)? I expected this to be possible by overriding ViewPager2.onInterceptTouchEvent() but since ViewPager2 is final…
ccpizza
  • 28,968
  • 18
  • 162
  • 169
1
2 3
49 50