1

I have nested ViewPagers and RecyclerViews as depicted in the image:

enter image description here

Requirements:

  • The first level ViewPager1 swipes from left to right. It uses FragmentStatePagerAdapter.
  • The first level RecyclerView1 scrolls vertically.
  • 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.
  • The second level RecyclerView does not scroll - it simply wraps a list of dynamic items

What I have working so far:

  1. The first level ViewPager1 and RecyclerView1 works as intended.
  2. The ViewPager2 does not show because its height is defined as "wrap_content"
  3. The ViewPager2/RecyclerView2 prevents RecyclerView1 to scroll up/down.

What I have tried:

  1. 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.
  2. 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.
  3. Overriding OnMeasure as described here 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?

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.

EDIT

  1. 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.
  2. Setting RecyclerView2.setNestedScrollingEnabled(false) fixed the vertical scrolling. However, now, the ViewPager2's touch behaviour is interfering with ViewPager1's
  3. 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.
  4. 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.
  5. 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.

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.

chaser
  • 3,107
  • 4
  • 29
  • 34
  • u might not be created fragments for all views you have created – Rashiq Feb 05 '18 at 07:27
  • I've checked, all fragments have been created for the first level viewpager. But problem is the second level ViewPager which does not use fragment, it uses views. And the views definitely are there, IF I set the ViewPager's heigh to say something like 500dp – chaser Feb 05 '18 at 07:49
  • stuck or lazziness while scrolling viewpager would be mostly due to not attaching improper views to fragment or other views. just check if you have attached layout for fragments and initialized in oncreateview – Rashiq Feb 05 '18 at 09:12
  • After extensive testing I know what the problem is. The created ViewHolder1 which holds ViewPager2 sees that ViewPager2's height is 0 because ViewPager2's content is generated during the instantiateItem (after the ViewHolder1 creation/binding process). After the ViewPager2's content is instantiated and is about to attach to the UI thread, it runs the onMeasure method to set the actual size. But by then it is too late, because the RecyclerView only knows the ViewPager's height of 0... now in terms of the solution, I'm not exactly sure yet... maybe someone can lead me there. – chaser Feb 05 '18 at 11:00
  • ok... so the problem would be, your recycler view is not wraping with height.. checkout the answr for this below reference where you can set recylcer views height dynamically... [link] (https://stackoverflow.com/questions/48110244/listview-and-recyclerview-doesnot-wrap-content-with-count) – Rashiq Feb 05 '18 at 11:08
  • have a look at my solution here: https://stackoverflow.com/a/39595010/3451697 – Harneev Feb 16 '18 at 04:52

0 Answers0