Skip to main content

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.

android-viewpager2
Filter by
Sorted by
Tagged with
642 votes
43 answers
440k views

Why `PagerAdapter::notifyDataSetChanged` is not updating the View?

I'm using the ViewPager from the compatibility library. I have succussfully got it displaying several views which I can page through. However, I'm having a hard time figuring out how to update the ...
C0deAttack's user avatar
  • 24.6k
174 votes
8 answers
65k views

How to disable swiping in ViewPager2?

Is it possible to enable-disable swiping in new android viewpager2 component?
murgupluoglu's user avatar
  • 6,964
153 votes
13 answers
215k views

Proper implementation of ViewPager2 in Android

I came to know about ViewPager2 and tried to implement it, but didn't find any proper example. Can anyone tell me how can I use it. I am looking for proper usage, not an example.
Pratik Butani's user avatar
102 votes
13 answers
121k views

How to use TabLayout with ViewPager2 in Android

I want to use com.google.android.material.tabs.TabLayout component with Android's new ViewPager implementation androidx.viewpager2.widget.ViewPager2. However, the setupWithViewPager(..) method ...
Ugurcan Yildirim's user avatar
56 votes
23 answers
48k views

Pages must fill the whole ViewPager2 (use match_parent)

I have an item layout where I display an image, product name, and product image. I must display image in 1:1.5 ration using constraint layout. But when I load a small image, below texts not displaying....
Mahesh Babariya's user avatar
46 votes
5 answers
8k views

Viewpager2 with fragments and Jetpack navigation: Restore fragments instead of recreating them

I have a Viewpager2 inside a Fragment (lets call it HomeFragment). That Viewpager itself also contains Fragments. When I navigate away from the HomeFragment its view will be destroyed and when I ...
sunilson's user avatar
  • 1,469
43 votes
14 answers
30k views

ViewPager2 default position

I'm creating a slideshow with ViewPager2. For example, the slideshow has 3 items and I want to show the second item when the activity opens. I use setCurrentItem(int item, boolean smoothScroll) method ...
hosseinAmini's user avatar
  • 2,314
40 votes
19 answers
35k views

ViewPager2 with differing item heights and WRAP_CONTENT

There are a few posts on getting ViewPager to work with varying height items that center around extending ViewPager itself to modify its onMeasure to support this. However, given that ViewPager2 is ...
Meetarp's user avatar
  • 2,451
33 votes
2 answers
14k views

remove fragment in viewPager2 use FragmentStateAdapter, but still display

I have a viewPager2 and FragmentStateAdapter, and there are Fragement1, 2,3 4, I am in fragment2, and want to remove fragment3, and display fragment4 after fragment2. The problem is it always show me ...
Xianwei's user avatar
  • 2,491
30 votes
5 answers
16k views

Change ViewPager2 Scroll Speed when sliding programmatically

I know there is way to change animation duration of ViewPager programmatical slide (here). But its not working on ViewPager2 I tried this: try { final Field scrollerField = ViewPager2....
Saeed Arianmanesh's user avatar
29 votes
6 answers
23k views

Android ViewPager2 setPageMargin unresolved

I want to implement Carousel using View Pager2 with preview of left and right page like this: Initially I was using view pager1 which supported. Now I think it's removed viewPagerhost....
Hitesh Sahu's user avatar
  • 44.1k
25 votes
5 answers
10k views

How to fix 'Design assumption violated' error in ViewPager2?

I'm using ViewPager2, Kotlin and AndroidX. When the adapter is not at index 0 and I change the adapter list and set current item to index 0 the exception is thrown. java.lang.IllegalStateException: ...
Amir's user avatar
  • 1,755
23 votes
7 answers
8k views

Remove ViewPager2 Overscroll animation

Can't find a way to remove the ViewPager2 overscroll shadow animation. I know on ViewPager, you can directly just set the overscrollMode attribute to never, however, it does not work on ViewPager2 ...
Daniel Kim's user avatar
22 votes
6 answers
12k views

Fragment no longer exists for key FragmentStateAdapter with Viewpager2

I am using ViewPager2 for my project. I need to use nested fragments inside a fragment with viewpager2. it works like charm until I try to navigate between fragments(not nested ones). After the first ...
Eren Tüfekçi's user avatar
21 votes
2 answers
7k views

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<Fragment>() ...
Manuel Munoz's user avatar
20 votes
9 answers
10k views

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 ...
patel dhaval r's user avatar
20 votes
4 answers
4k views

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 ...
waseefakhtar's user avatar
  • 1,403
19 votes
0 answers
4k views

Android: How to intercept touch event in ViewPager2

How to prevent swiping in androidx ViewPager2 when the touch event is coming from inside a nested view (e.g. a HorizontalScrollView, etc)? I expected this to be possible by overriding ViewPager2....
ccpizza's user avatar
  • 30.8k
18 votes
4 answers
14k views

I can't find PageChangeListener in ViewPager2.?

ViewPager2 PageChangeListener is not working like normal ViewPager.?
Unaisul Hadi's user avatar
18 votes
1 answer
3k views

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() { @...
anthony's user avatar
  • 7,703
16 votes
2 answers
9k views

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'...
Adam Ma's user avatar
  • 668
15 votes
3 answers
15k views

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 ...
DenBond's user avatar
  • 174
15 votes
5 answers
16k views

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<Integer> ...
Vodet's user avatar
  • 1,500
15 votes
3 answers
27k views

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 { ...
mdDroid's user avatar
  • 3,175
15 votes
10 answers
13k views

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. ...
ban-geoengineering's user avatar
15 votes
3 answers
4k views

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 ...
grolschie's user avatar
  • 2,163
15 votes
1 answer
1k views

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 ...
Oscar Hernandez's user avatar
14 votes
4 answers
10k views

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 '...
user avatar
14 votes
1 answer
4k views

TabLayoutMediator not preserving TabItem attributes

I have a simple setup using ViewPager2 and TabLayout with preset TabItems: ... <com.google.android.material.tabs.TabLayout android:id="@+id/tabs" android:...
Evgeni Roitburg's user avatar
13 votes
2 answers
15k views

Open ViewPager2 to particular position without scrolling

I have implemented a recyclerview with some videos (say 10). Clicking on any item opens a viewpager2 which is on some other activity with all the items and the clicked one (from recyclerview) is shown....
Narendra Pal's user avatar
  • 6,574
13 votes
3 answers
7k views

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 ...
kovac777's user avatar
  • 730
13 votes
3 answers
3k views

Why is the Android ViewPager2 class final?

While using ViewPager2 in my current project, I wanted to use setOnTouchListener, but I was unable to get any events. I decided to extend the ViewPager2 class to override the touch event methods, but ...
Ahmed Elshaer's user avatar
12 votes
2 answers
7k views

ViewPager2 inside a fragment leaks after replacing the fragment it's in by Navigation Component navigate

At first, i had the issue for ViewPager2 inside a tab of BottomNavigationView and data binding , data binding also leaks with ViewPager2 and should be nulled in onDestroyView, leaking and managed to ...
Thracian's user avatar
  • 58.8k
12 votes
6 answers
13k views

How to add endless/infinite scroll to ViewPager2?

I have a FragmentStateAdapter that is attached to new ViewPager2. In the previous ViewPager implementation there were number of ways to implement endless/infinite scroll. How to do so in ViewPager2? ...
Ali Yucel Akgul's user avatar
11 votes
2 answers
3k views

BottomSheetBehavior with ViewPager2 can't be scrolled down by nested RecyclerView scroll

I have a view that acts like BottomSheetBehavior and this view has ViewPager2 inside. Each ViewPager2's page is a vertical RecyclerView. The issue is that BottomSheet doesn't scroll down when current ...
SimpleAndroid's user avatar
11 votes
4 answers
4k views

How to make ViewPager2 less sensitive?

I'm using ViewPager2 in my App, it has three pages and each page show different contents, the issue is that it's too sensible that the ViewPager will change the pages even if someone is scrolling down ...
NiceToMytyuk's user avatar
  • 4,035
11 votes
4 answers
6k views

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?
Shamil's user avatar
  • 113
11 votes
3 answers
6k views

Prevent Swiping of ViewPager2 when onTouch of inner view

I have a ViewPager2 with a fragment inside, in the fragment, I have a custom view with certain touch logic that involves moving the finger. how do I prevent the ViewPager from swiping while the ...
Vasili Fedotov's user avatar
10 votes
3 answers
17k views

Vertical ViewPager2 with RecyclerView Scrolling Issue

I'm using a ViewPager2 with two fragments in a vertical orientation. When the user swipes down to the second fragment, there is a RecyclerView that scrolls content in the same vertical direction. The ...
Calvin Rai's user avatar
10 votes
2 answers
6k views

ViewPager2 not able to dynamically add remove fragment

Removing/Adding fragments at index results in unexpected behaviour in Viewpager2. This was not possible with ViewPager but expected to work with Viewpager2. It causes duplicate fragments and out of ...
Abhishek Bansal's user avatar
10 votes
1 answer
3k views

How to make ViewPager2 less sensitive to swipe? [duplicate]

My ViewPager2 contains fragments with RecyclerView. When I scroll down and up inside adapter, which has a few items (not cover whole screen), it very often "swype" to next or previous fragment in view ...
Michalsx's user avatar
  • 3,541
10 votes
2 answers
4k views

ViewPager2/Tabs problem with ViewModel state

I am following the MVVM pattern - meaning I have a ViewModel for each Fragment. I added two tabs by using ViewPager2. My adapter looks like this: @Override public Fragment createFragment(int ...
user123456789's user avatar
10 votes
2 answers
9k views

E/iardini.error0: Invalid ID 0x00000001 - Error resource ID

I am currently stuck on an error that continues to slow down the performance of my application. The error does not cause the application to close or crash, or to write an exception to the console. The ...
G. Ciardini's user avatar
  • 1,277
10 votes
0 answers
885 views

"Expected the adapter to be 'fresh' while restoring state" in Viewpager2 FragmentStateAdapter

When I try to re-use a Viewpager2 FragmentStateAdapter, I get this error. I already tried to set the adapter to null in onDestroyView but this does not help. This occurs because I am using the ...
sunilson's user avatar
  • 1,469
10 votes
1 answer
6k views

Fragment disappears with ViewPager2 and FragmentStateAdapter

I'm building an app using ViewPager2 that displays different fragments when swiped. My adapter extends FragmentStateAdapter as described here. When I put my app into the background and then resume ...
rstockbridge's user avatar
9 votes
2 answers
17k views

ViewPager2 - How to use registerOnPageChangeCallback to check view pager item position

How can I use ViewPager2's registerOnPageChangeCallback facility to check the position of the currently visible ViewPAger2 fragment? I know this will consists of an if/when statement but don't know ...
wbk727's user avatar
  • 8,270
9 votes
1 answer
9k views

Unresolved reference: TabLayoutMediator [duplicate]

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 ...
Athena's user avatar
  • 543
9 votes
1 answer
4k views

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 ...
Antonis Radz's user avatar
  • 3,097
9 votes
1 answer
28k views

Why FragmentStateAdapter not work in ViewPager2? [duplicate]

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 ...
Alexei's user avatar
  • 15.2k
9 votes
3 answers
4k views

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 ...
B.Ondrej's user avatar
  • 391

1
2 3 4 5
15