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

How to fix Scrolling issue of horizontal ViewPager2 and RecyclerView that are inside a vertical RecyclerView?

I have a RecyclerView (say, rootRecyclerView) that can have different kinds of rows depending on some API response. I implemented one of them is a horizontal ViewPager2 and another one is implemented with horizontal RecyclerView (say,…
4
votes
3 answers

Android ViewPager2 with fragment containing a recyclerview not scrolling

I'm using ViewPager2 with FragmentStateAdapter to bind fragments. Each of my fragment has 3 recycler views laid out vertically. The problem is I'm not able to scroll the recycler view inside the fragments and also when I try to scroll the content…
rko
  • 203
  • 3
  • 10
4
votes
5 answers

Infinite scrolling in viewpager2

How can we implement infinite scrolling in ViewPager2 As there is no OnPageChangeListener() in this pager. But there is setPageTransformer() Listener which get callback whenever we change page. So any solution for viewpager2 problem Note : I'm using…
nikhil lohar
  • 139
  • 3
  • 14
4
votes
1 answer

How do I implement a method to get and return the LifeCycle

I have issue with the FragmentStateAdapter asking for LifeCycle, kindly help how I can set and pass the LifeCycle in the line of code below. viewPager2.adapter = myViewPagerAdapter(supportFragmentManager, getCurrentLifeCycle()) Here is the…
3
votes
0 answers

How to handle error in paging3 using Remote mediator with viewPager2

I am using RemoteMediator to populate my Viewpager2. The view pager contains weekly data and it is in RTL format because i have to show data in descending order. current week, last week.... Please refer below pictures. So i am able to fetch data…
Nightswatch
  • 133
  • 1
  • 8
3
votes
2 answers

RecyclerView onClick event is not working with SwipeRefreshLayout?

Before using FragmentStateAdapter. I was using FragmentPagerAdapter then everything was fine but when I migrated from FragmentPagerAdapter to FragmentStateAdapter to reduce memory usage. The issue that I'm facing is that when RecyclerView is at the…
3
votes
0 answers

ExoPlayer occupying memory even after releasing

In the main activity of my app, I show a number of cards to user. when he clicks on any of them, a fragment is opened which contains a ViewPager2. The fragments inside the viewPager2 each contains an ExoPlayer and plays a related video when…
3
votes
2 answers

ViewPager2 setCurrentItem bug workaround?

This reported bug is causing me problems; https://issuetracker.google.com/issues/183847283 When combined with FragmentStateAdapter, ViewPager2.setCurrentItem doesn't always work. The TabLayout correctly changes (if you have one), but the ViewPager2…
3
votes
1 answer

How to stop a video when the page is not visible in ViewPager2

I have a app where I make a API call and get a arraylist of videos, which I play in a viewpager using a adapter. When I scroll the viewpager, the video playing before still keeps playing and even after I scroll again. I want it so that the video…
3
votes
1 answer

android, crash on transition for my fragment

I have a random crash on my application, and look they are on android internal code. One is: java.lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout at…
Perry
  • 1,113
  • 2
  • 10
  • 22
3
votes
2 answers

How to use ItemTouchHelper.SimpleCallback with ViewPager2?

I have created a image slider using ViewPager2, I need a functionality that when I swipe up the image should be removed. I already worked with RecyclerView. So I remember I can use ItemTouchHelper.SimpleCallback for swipe to remove functionality.…
3
votes
1 answer

AppBar not scrolling with nested ViewPager2

I have a view hierarchy as shown in the image below. I'm getting strange scroll behaviors like, If I scroll (drag slowly or fling) from Area 1 the AppBar collapses along with it. This is fine. But if I drag slowly from Area 2 the AppBar does not…
3
votes
2 answers

Replacement for BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

androidx.fragment.app.FragmentStatePagerAdapter is deprecated and developers are urged to switch to androidx.viewpager2.adapter.FragmentStateAdapter How can we get the same functionality with FragmentStateAdapter that…
3
votes
1 answer

What is the difference between each constructor when extending FragmentStateAdapter?

I'm learning about fragments, and in the app that I'm making I have a bottom navigation bar, and the first one has a fragment with a ViewPager2. For the viewpager2 I created a custom adapter that extends FragmentStateAdapter, first I used the…
3
votes
0 answers

android viewpager2 CreateFragment() selecting multiple cases in when(position)

I've been learning about TabLayout and ViewPager2. I think it's pretty neat. Its exactly what I was looking for but when I went to applied it in my app I noticed something strange. In the PagerAdapter class I see multiple cases being selected. So…
dbarnes
  • 439
  • 1
  • 3
  • 11