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

When to do fragment preparation work in ViewPage2

I am trying to migrate my app from the legacy ViewPager to ViewPage2. And I found that unlike ViewPager, ViewPager2 will not call fragment's onCreate() or onCreateView when the fragment is not visible. For example, user is on Fragment[N], when using…
Robin
  • 10,052
  • 6
  • 31
  • 52
0
votes
2 answers

Androidx ViewPager2 incompatible types: ViewPagerAdapter cannot be converted to Adapter

I am using ViewPager2, and I get the following error on viewPager2.setAdapter: incompatible types: ViewPagerAdapter cannot be converted to Adapter MainActivty viewPagerAdapter = new…
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
0
votes
0 answers

IndexOutOfBoundsException in ViewPager with two tabs

I have two tabs in which I add two different lists of data, both tabs share a single recyclerview, so at my viewpager adapter, I just create a new instance to populate the data From the View val allProducts = completeProductList val…
SNM
  • 5,625
  • 9
  • 28
  • 77
0
votes
0 answers

ViewPager with two tabs and a shared recyclerview onClicks on different item

So, I have a viewpager with two tabs, each tab needs its own set of data, so I have done this in order to send two different lists to the viewpager container, and then place this list in a shared recyclerview which each tab will display So , this…
0
votes
1 answer

MapView/SupportMapFragment graphic glitch while using ViewPager2

I've been trying to use MapView inside a ViewPager fragment and I've encountered a weird bug. MapView keeps glitching like it's trying to re-render and stretching the map weirdly; however, while using zoom/gestures, it's fine. The moment it stops…
0
votes
1 answer

Wrong 2nd argument type , Found 'androidx.lifecycle.Lifecycle', required 'int' Error

I'm getting this error when trying to implement a viewpager2 here is the relevant code: public class ViewPagerAdapter extends FragmentPagerAdapter { private Fragment[] childFragments; public ViewPagerAdapter(@NonNull FragmentManager fragmentManager,…
0
votes
1 answer

Change image view background of View Pager2 selected image in android

I am trying to changing the background imageview of viewpager2 selected imageview in android. But in my case not changing image view background, when viewpager2 selected. Below code to change image view background. final List names = new…
amar544
  • 43
  • 9
0
votes
1 answer

Animated (textView) keeps cancelling when swiping between fragments within Fragment State Adapter

I am developing an app on android studio (Java) I have created a tabLayout that swipes between two fragments using a viewPager2 + Fragment State Adapter On fragment02, I have animated a textView. When I swipe onto the fragment the animation plays…
0
votes
1 answer

How to disable destroying of fragments in the new ViewPager2?

In the FragmentPagerAdapter I can override destroyItem method and just remove line super.destroyItem(container, position, object); @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { …
Dyno Cris
  • 1,823
  • 1
  • 11
  • 20
0
votes
1 answer

Setting RecyclerView adapter into ViewPager2

I've created a RecyclerView with Horizontal Scrolling and PageSnapHelper. Now I think to replace RecyclerView with ViewPager2.? Can I simply set RecyclerView Adapter I've created earlier for new ViewPager2.? Adapter Class goes here class…
0
votes
2 answers

How to switch automatically between ViewPager2 pages

How to move ViewPager2 pages automatically?
Bincy Baby
  • 3,941
  • 5
  • 36
  • 62
0
votes
1 answer

Fill fragment that is not visible with data?

I've got an activity and I'm using FragmentStateAdapter class (ViewPager2) to slide between two fragments, similar like in https://developer.android.com/training/animation/screen-slide-2. My current problem is, that when I try to access onto view…
icouldin
  • 205
  • 2
  • 12
0
votes
0 answers

ViewPager - Preview only 1 right page but not the last page

I setup my ViewPager to only show preview of 1 page on the right side using setPadding and pageMargin: viewpager.setPadding(smallSpace, smallSpace, largeRightPadding, smallSpace) viewpager.pageMargin = normalSpace viewpager.offscreenPageLimit =…
Tam Huynh
  • 2,026
  • 1
  • 16
  • 20
0
votes
1 answer

ViewPager2 - Slider transition sliding too fast

I have implemented from the documentation of viewpager2 the follow animation private const val MIN_SCALE = 0.85f private const val MIN_ALPHA = 0.5f class ZoomOutPageTransformer : ViewPager2.PageTransformer { override fun transformPage(view:…
SNM
  • 5,625
  • 9
  • 28
  • 77
0
votes
0 answers

java.lang.NullPointerException on clearAnimation()

How this crash is possible? It works 99% of the time and sometimes there is this crash... Caused by java.lang.NullPointerException Attempt to invoke virtual method 'void android.view.View.clearAnimation()' on a null object…
anthony
  • 7,653
  • 8
  • 49
  • 101