I use a ViewPager2 inside another ViewPager2. Because of this, the slide only works for the root ViewPager. How to make both ViewPager work?
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.
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.
I have tested the class, but the nestedScrollHost class does not use viewPager,this class uses a recycler instead of a viewpagers .
mPager.setNestedScrollingEnabled(true) for child views
© 2022 - 2024 — McMap. All rights reserved.