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
714
questions
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 ...
174
votes
8
answers
65k
views
How to disable swiping in ViewPager2?
Is it possible to enable-disable swiping in new android viewpager2 component?
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.
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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....
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....
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: ...
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
...
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 ...
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>()
...
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 ...
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 ...
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....
18
votes
4
answers
14k
views
I can't find PageChangeListener in ViewPager2.?
ViewPager2 PageChangeListener is not working like normal ViewPager.?
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() {
@...
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'...
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 ...
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> ...
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 {
...
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.
...
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 ...
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 ...
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 '...
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:...
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....
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 ...
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 ...
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 ...
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?
...
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 ...
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 ...
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?
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...