Foggy day

Android - Viewpager2 inside Viewpager2 본문

Android

Android - Viewpager2 inside Viewpager2

jinhan38 2021. 1. 1. 13:41

 

 

when there is Viewpager2  inside Viewpager2, ViewPager2 inside can't be swiped. 

 

You can see explanation of it in Google Doc.

 


 

developer.android.com/training/animation/vp2-migration#nested-scrollables

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 wrappger layout.

 


 

In this case, the solution is to call requestDisallowInterceptTouchEvent(true) in setOnTouchListener of child viewpager2.

 

  childViewPager2.setOnTouchListener((view, motionEvent) -> {
            childViewPager2.requestDisallowInterceptTouchEvent(true);
            return false;
        });

 

 

But, There is another problem. If you have scrollView in ParentViewPager2 and call requestDisallowInterceptTouchEvent(true) in ChildViewPager2 and touch ChildViewPager2, you can't scroll scrollview.

Because when you touch ChildViewPager2, requestDisallowInterceptTouchEvent() called ChildViewPager2 intercept touchEvent.

 

To solve this problem, you should find another Solution.