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
19
votes
8 answers

ViewPager2 crash

I am using Advance Navigation Component with BottomNavigationView. In one tab I have ViewPager2. When I clicked on the tab for the first time, it worked fine. Although the second time, come on that tab application keep crashing. Below is the crash…
19
votes
2 answers

FragmentStateAdapter not recreating currentFragment after notifyDataSetChanged

I have a FragmentStateAdapter as the adapter of a ViewPager2. class DefaultFragmentStateAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { var items = listOf() set(value) { field = value …
Manuel Munoz
  • 753
  • 1
  • 6
  • 15
19
votes
3 answers

Page(s) contain a ViewGroup with a LayoutTransition (or animateLayoutChanges="true"), which interferes with the scrolling animation

I use fragments with ViewPager2, and I notice two relevant IllegalStateExceptions in production (I can't reproduce it myself) occurring in devices like Xiaomi, Yulong, asus, vivo running Android 8 or 9: Fatal Exception:…
waseefakhtar
  • 1,373
  • 2
  • 24
  • 47
18
votes
4 answers

I can't find PageChangeListener in ViewPager2.?

ViewPager2 PageChangeListener is not working like normal ViewPager.?
16
votes
1 answer

HowTo Nest ViewPager2 within a fragment

I am trying to put a ViewPager2 with 2 tabs(2 fragments) within my main fragment. My fragment class extends Fragment and I need it to contain a ViewPager2 with fragments implemented inside. I couldn't find guides explaining this clearly on the web…
Adam Ma
  • 624
  • 5
  • 16
16
votes
1 answer

ViewPager2: setOnTouchListener() doesn't call

I'm migrating my ViewPager to the new ViewPager2. Unfortunately with this new class, the setOnTouchListener is never called. mViewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View…
anthony
  • 7,653
  • 8
  • 49
  • 101
15
votes
3 answers

java.lang.IllegalStateException: Page can only be offset by a positive amount

I am getting this error in my Pre-Launch Reports, but it doesn't show any references to my .java files. Can anyone please shed some light on this? I'm using a ViewPager2 (1.0.0) with page transformations. Issue: java.lang.IllegalStateException: Page…
grolschie
  • 2,163
  • 2
  • 12
  • 29
15
votes
3 answers

View Pager with previous and next item smaller in size with infinite scroll

Want to create the view pager same as following UI, applied custom transformer but not working. ViewPager.java public class MyViewPager extends ViewPager implements ViewPager.PageTransformer { public static final String TAG =…
15
votes
10 answers

How to wrap height of Android ViewPager2 to height of current item?

This question is for the new ViewPager2 class. There is a similar question for the old ViewPager, but the solution requires extending ViewPager. However, ViewPager2 is final so cannot be extended. In my situation, I have a ViewPager2 that contains…
15
votes
5 answers

ViewPager2 with horizontal scrollView inside

I implemented the new ViewPager for my project. The viewPager2 contains a list of fragment private class ViewPagerAdapter extends FragmentStateAdapter { private ArrayList classifiedIds; ViewPagerAdapter(@NonNull Fragment…
14
votes
1 answer

BottomSheet + ViewPager2 drag to hide not works

I got troubles and I think it is CoordinatorLayout's fault but not sure. I'm using ViewPager2 inside a ConstraintLayout and I use the CoordinatorLayout like BottomSheet. But when I drag to hide it not works good. I replaced the ViewPager2 by…
13
votes
3 answers

Disable animation in viewpager2

I have viewpager2 and adapter for him that extends FragmentStateAdapter. I want user to go to another page only by clicking on tablayout. I have disabled user input for this viewpager2. But when I click on tab, there is animation of fast swiping…
DenBondd
  • 155
  • 1
  • 1
  • 9
13
votes
4 answers

How to create a custom view extending from viewpager2?

I would like to create my own version of custom ViewPager2 view from extending from the original Viewpager2, but just had a hard time doing it. I get errors such as Cannot inherit from final 'androidx.viewpager2.widget.ViewPager2' Is there a…
user12351740
13
votes
1 answer

TabLayoutMediator not preserving TabItem attributes

I have a simple setup using ViewPager2 and TabLayout with preset TabItems: ...
13
votes
3 answers

ViewPager2 | View.ClickListener not called

I use new android widget ViewPager2 version 1.0.0-alpha03 and when I set click listener on it method onClick() not called. My Actvity class: class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { …
kovac777
  • 730
  • 6
  • 19
1
2
3
49 50