How to use a ViewPager2 inside another ViewPager2
Asked Answered
S

4

11

I use a ViewPager2 inside another ViewPager2. Because of this, the slide only works for the root ViewPager. How to make both ViewPager work?

Straightjacket answered 25/5, 2020 at 22:1 Comment(0)
F
16

As the documentation says :

"Support nested scrollable elements

ViewPager2 does not natively support nested scroll views in cases where the scroll view has the same orientation as the ViewPager2 object that contains it. For example, scrolling would not work for a vertical scroll view inside a vertically-oriented ViewPager2 object.

To support a scroll view inside a ViewPager2 object with the same orientation, you must call requestDisallowInterceptTouchEvent() on the ViewPager2 object when you expect to scroll the nested element instead. The ViewPager2 nested scrolling sample demonstrates one way of solving this problem with a versatile custom wrapper layout."

I have tried it myself and it works just fine, you need to use this class.

And this is the link to the documentation.

Fulvous answered 30/7, 2020 at 12:55 Comment(0)
A
0

Creating a custom frame layout might be useful in some cases, but remember if you have any functionality in the child viewpager2 that is triggered when you click on the options in the child viewpager2 , it will not work properly cause it won't be able to detect yours on click functionality. For example : if you have a button inside the child vewpager2, which navigates you to other activities. It will not work. as the frame layout has access to touch and it will intercept the touch to the button. hence it will likely as unclickable. I myself have tried this and have faced this problem. Still searching for the best solution to this problem of using viewpager2 inside viewpager2. I have tried using viewpager in the parent instead of viewpager2, so it works well, if you want just to make it work properly then you can use viewpager. But still, a solution to this problem needs to be done.

Algonquian answered 14/11, 2023 at 5:34 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Aquiline
D
-1

I have tested the class, but the nestedScrollHost class does not use viewPager,this class uses a recycler instead of a viewpagers .

Daviddavida answered 25/1, 2023 at 12:46 Comment(2)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewHanker
It is not an answer to the question it is a comment since stackover flow does not allow me to answer without reputation and thanks to your score on my question even less now.Daviddavida
H
-2

mPager.setNestedScrollingEnabled(true) for child views

Hurst answered 25/2, 2021 at 17:7 Comment(1)
This solution does not work. This solutions works fine: github.com/android/views-widgets-samples/blob/master/ViewPager2/…Keeton

© 2022 - 2024 — McMap. All rights reserved.