title: Nested RecyclerView and ViewPager content wrapping and scrolling don't play nice together
body: <p>I have nested ViewPagers and RecyclerViews as depicted in the image:</p>
  
  <p><a href="https://i.stack.imgur.com/ggRnl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ggRnl.png" alt="enter image description here"></a></p>
  
  <p>Requirements:</p>
  
  <ul>
  <li>The first level ViewPager1 swipes from left to right. It uses FragmentStatePagerAdapter.</li>
  <li>The first level RecyclerView1 scrolls vertically.</li>
  <li>The second level ViewPager2 does not swipe - the swipe motion is controlled by a TabLayout. It uses a custom PagerAdapter that returns a View as a page.</li>
  <li>The second level RecyclerView does not scroll - it simply wraps a list of dynamic items</li>
  </ul>
  
  <p>What I have working so far:</p>
  
  <ol>
  <li>The first level ViewPager1 and RecyclerView1 works as intended.</li>
  <li>The ViewPager2 does not show because its height is defined as "wrap_content"</li>
  <li>The ViewPager2/RecyclerView2 prevents RecyclerView1 to scroll up/down.</li>
  </ol>
  
  <p>What I have tried:</p>
  
  <ol>
  <li>Setting RecyclerView1.setNestedScrollingEnabled(false) stops it from passing the onTouch event to its children, but because the ViewPager2/RecyclerView2 wraps its content, it does not know what the size it needs to scroll.</li>
  <li>Setting the ViewPager2 to a fixed height solves the scrolling problem. But because it is a fixed height, the content of RecyclerView2 is cut off.</li>
  <li>Overriding OnMeasure as described <a href="https://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content">here</a> makes ViewPager2's content wrap, perfectly, but the scrolling no longer work again. I assume it is because OnMeasure is called "after" the View has already been attached?</li>
  </ol>
  
  <p>So basically I need help on how to get the content to wrap but in such a way that RecyclerView 1 knows what the height is so that it can scroll.</p>
  
  <p><strong>EDIT</strong></p>
  
  <ol start="4">
  <li>It turns out I was totally off base with point 3. The OnMeasure workaround DOES work as intended and the scrolling problem is NOT caused by recyclerView not knowing the height. It in fact does. The reason why it doesn't scroll is due to multiple nested scrollable view groups. I found this out by putting Log.i on onTouchEvent() and onInterceptTouchEvent() on all the scrollable view groups. Some surface of the views work, but if the surface has another scrollable child, it starts to cause problems.</li>
  <li>Setting RecyclerView2.setNestedScrollingEnabled(false) fixed the vertical scrolling. However, now, the ViewPager2's touch behaviour is interfering with ViewPager1's</li>
  <li>On closer inspection, ViewPager1 intercepts touch event when hitting non-scrollable surface, causing the ViewPager1 to call its onTouchEvent() to scroll left and right. However, if I start the touch event over a the ViewPager2's surface, ViewPager1 never intercept and it never handles the swipe left to right.</li>
  <li>Unlike a RecyclerView, there is no simple method to disable nestedScrolling. So I tried disabling ViewPager2, but that didn't work and caused the inside views such as buttons not clickable.</li>
  <li>I tried to return false in ViewPager2's OnTouchEvent so that it bubbles up the chain, but still, the ViewPager1's OnTouchEvent is never fired.</li>
  </ol>
  
  <p>So I'm stuck again, how do I pass the touch event to the parent when the parent did not intercept the event when it should have. Again, I'm assuming, and again I might be off-base, that ViewPager1 might not intercept because ViewPager2 has requested a disallowInterceptTouchEvent() somewhere in its code? I just don't know where or how to begin to fix this problem.</p>