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
9
votes
1 answer

How to use PagerTabStrip with ViewPager2

I want to use ViewPager2 because it is supposed to replace ViewPager and I hope to get rid of some problems I've had with ViewPager. I want to do this:
9
votes
1 answer

Why FragmentStateAdapter not work in ViewPager2?

Android Studio 3.6 I want in my activity to use 2 fragments inside ViewPager2. Here my activity: import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.viewpager2.widget.ViewPager2; public class…
Alexei
  • 14,350
  • 37
  • 121
  • 240
9
votes
1 answer

Unresolved reference: TabLayoutMediator

I am developing an android application which needs a TabLayout bind to a ViewPager2. Reading this and following the code in here, I need to use TabLayoutMediator. But when importing it, I face Unresolved reference error. I know what this error…
Athena
  • 543
  • 10
  • 30
9
votes
3 answers

viewpager2 interaction with SwipeRefreshLayout

I have viewpager2 with 4 fragments. 3 of them have SwipeRefreshLayout to refresh async task data in particular fragments. When using SwipeRefreshLayout and viewpager2 the gestures are somehow conflicting. ie. swype down to refresh makes screen so…
B.Ondrej
  • 381
  • 4
  • 14
9
votes
1 answer

Preload next page in ViewHolder2 like it was automatically done in ViewPager

How to preload next page and page before in ViewPager2? My app uses list from api in each ViewPager2 tab screen. It is good user experience when next page is loaded before user swipes to it. On old ViewPager it was done automatically
Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
8
votes
2 answers

fragments inside ViewPager2 are recreated when coming back to parent fragment

I have a ViewPager2 in one of my fragments (let's call it fragment A). the viewpager uses a FragmentStateAdapter to create a number of fragments as the children (let's call these fragments children). when the user clicks on a button in fragment A, I…
8
votes
2 answers

ViewPager2 not working properly with Fragments and click events

I just wanna know if I'm doing something wrong since I'm kinda new to all this. If there is anything else that you'll like me to add just let me know. This is the repo branch where I'm trying to implement the ViewPager if you wanna see all the…
8
votes
3 answers

How to use a ViewPager2 inside another ViewPager2

I use a ViewPager2 inside another ViewPager2. Because of this, the slide only works for the root ViewPager. How to make both ViewPager work?
8
votes
2 answers

Why notifyDataSetChanged blow up PagerTransformer in ViewPager2?

I am using ViewPager2 version 1.0.0-beta05, with a RecyclerView.Adapter, and ZoomOutPageTransformer, I found that when we call notifyDataSetChanged, the ViewPager view blow up. In Version 1.0.0-alpha01, they said that notifyDataSetChanged fully…
8
votes
0 answers

Offscreenpagelimit vs setItemViewCacheSize ViewPager2

I'm using ViewPager2, newest version 'androidx.viewpager2:viewpager2:1.0.0-beta04' I have 10 pages and each page is Fragment. If i set viewpager2.offscreenPageLimit = 1 I have to scroll to page 5 to make the first page destroy. What happened???, the…
8
votes
4 answers

Android - Program type already present com.transition.R while importing ViewPager2 library

Gradle is throwing Program type already present: androidx.transition.R" after importing androidx.viewpager2:viewpager2:1.0.0-beta02 implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02' implementation…
Dimitri
  • 123
  • 1
  • 7
8
votes
1 answer

Fragments in ViewPager2 does not respond to clicks if scroll position is 0

I am very glad that Google released ViewPager2 which is built on RecyclerView to solve a lot of issues that the old ViewPager has. I quickly replaced my old ViewPager codes to ViewPager2: Replace ViewPager with ViewPager2 in xml Replace…
7
votes
1 answer

FragmentManager is already executing transactions viewpager2

I am using 4 fragments with view pager 2. And got this error suddenly. The crash does not always appear. I have searched other problems but I could not find any solution about viewpager2. crash report from mobile could not get the full…
Niaz Sagor
  • 83
  • 1
  • 4
7
votes
2 answers

How to go to a position with view pager 2 that uses paging 3 to load data?

I am using ViewPager2 to display data that I fetch from a server and save to a Room database using the Paging 3 library. My question is, how do I navigate to a specific view pager item through code? If I use viewPager.setCurrentItem(position, false)…
axlrtr
  • 535
  • 6
  • 23
7
votes
5 answers

Viewpager2 and Fragments

ViewPager2 does not support direct child views I'm trying to transition between fragments using the following code but I get the above error when using viewpager2. Call in fragment 1 to transition to fragment…
M A F
  • 291
  • 4
  • 14