android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.04k stars 3.01k forks source link

Used OnTouchListener for nested scrolling in ViewPager2 #112

Open tunjid opened 4 years ago

tunjid commented 4 years ago

👋 👋 👋

This PR modifies the NestedScrollableHost to be a View.OnTouchListener rather than a ViewGroup that houses the nested scrolling child.

I don't think this is a better implementation per se, just more of a different one. I'm mostly curious about the design decisions that led to opting for nesting the scrollable child rather than using the View.OnTouchListener.

Off the top of my head, I can see the View.onTouchListener is a bit intrusive and limits custom touch interception on the nested scrollable View, however this is mitigated if this View were a RecyclerView as it could use the RecyclerView.OnItemTouchListener instead, which would be perfect for this example.

FWIW, this solution worked with nested ViewPagers, i.e I had ViewPager2 -> ViewPager2 -> HorizontalScrollView where the middle ViewPager2 had isUserInputEnabled = false and it worked a charm.