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
2 answers

Shared element transition to a fragment that contains a ViewPager

I am trying to perform a shared element transition from a RecyclerView item in Fragment A to Fragment B. The transition-names are set on the outermost CardViews in both layouts. My implementation is basically the same as the one in this…
4
votes
3 answers

FragmentStateAdapter not calling createFragment when notifyDataSetChanged is called

I am using a ViewPager2 together with a FragmentStateAdapter. public class MyAdapter extends FragmentStateAdapter { public MyAdapter(@NonNull Fragment fragment) { super(fragment); } @NonNull @Override public Fragment…
MaxxOr
  • 55
  • 2
  • 6
4
votes
1 answer

How to send data from Activity to Fragment with ViewPager2?

I'm trying to send data from MainActivity to one of the fragments which was created using ViewPager2. MainActivity: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); …
DenVebber
  • 153
  • 3
  • 11
4
votes
0 answers

Bottomsheet with Viewpager2 on Android

I am developing an app that uses a vertical bottom sheet. To be more specific, it is a LinearLayout with bottomSheetBehavior. This bottom sheet comprises two elements: a view pager that displays images and can be scrolled horizontally, and, below…
4
votes
1 answer

Android - ViewPager2 resets adapter position to 0 on calling Diffutil.dispatchUpdatesTo

I have a ViewPager2 which is set to swipe horizontally. I idea is to build an infinite scrolling pager. The class looks something like this: class SwipeAdapter(fragmentActivity: FragmentActivity): FragmentStateAdapter(fragmentActivity) I have…
4
votes
4 answers

how to override and display title of tabs in viewpager2 in tablayout

I am using a TabLayout inside a Fragment. The content of the TabLayout (made of 2 fragments) is displayed but not the title of the tabs. I believe it's a layout problem but i'm not sure on how to fix it. Also what would be the overrided method for…
eldocwho _tf
  • 49
  • 1
  • 3
4
votes
1 answer

Android ViewPager2 library throwing Transactiontoolarge exception onpause event

I am getting TransactionTooLarge exception if page size = 50 and I press the home button. I checked the FragmentStateAdapter and found that 'saveState()' method is finalized. Please help me how to resolve this. In ViewPager it was overriden by me…
4
votes
2 answers

ViewPager2 autoscroll until end of adapter itemCount

I have tried this a few different ways and I havent been able to get this viewpager to perform correctly. I am setting a viewpager2 with an adapter but part of the requirements are that the viewpager be manually swipe-able as well the base on a…
DevinM
  • 1,112
  • 1
  • 12
  • 29
4
votes
0 answers

viewpager2: java.lang.IllegalStateException: Expected the adapter to be 'fresh' while restoring state

Here is the more of stacktrace java.lang.IllegalStateException: Expected the adapter to be 'fresh' while restoring state. at androidx.viewpager2.adapter.FragmentStateAdapter.restoreState(FragmentStateAdapter.java:557) at…
Sujan Poudel
  • 794
  • 5
  • 16
4
votes
0 answers

Not able to navigate to next item in ViewPager2 using Dpad for accessibility

I have a horizontal ViewPager2 with items in it. I'm not able to navigate to next item in ViewPager2 using keyboard/dpad. When the same is replaced with RecyclerView, I'm able to navigate to next items. In both the cases, when swiped with two…
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
4
votes
1 answer

Android ViewPager2 how to retrieve the fragment onBindViewHolder

ViewPager release 1.0.0 version For a normal RecyclerView, holder.itemView gives me the view currently binding/ rendering. However, FragmentStateAdapter's holder.itemView only gives me a FrameLayout My ViewPager adapater: class…
4
votes
2 answers

OnTouchListener for ViewPager2

I recently changed the ViewPager in my application to a ViewPager2. I had set an onTouchListener to the viewPager object to detect gestures (onFling and onLongPress), as such: mViewPager.setOnTouchListener(new View.OnTouchListener() { @Override …
4
votes
2 answers

How to get Current Fragment in ViewPager2

i used this ViewPager2 dependency: implementation 'androidx.viewpager2:viewpager2:1.0.0' then i wrote code and make Adapter for FragmentStateAdapter like below: private inner class ViewPagerAdapter(fragmentManager: FragmentManager, …
Nanda Z
  • 1,604
  • 4
  • 15
  • 37
4
votes
2 answers

viewpager2 RTL not working with manifest supportsRtl=false

Is there any way to force ViewPager2 RTL direction? I have set android:layoutDirection="rtl" to viewpager2 but because of android:supportsRtl="false" in manifest it is still LTR. How can i solve this?
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44