Nested ScrollView Inner-First Scrolling Without the Fight
Nested scrolling is a recurring source of jank in Android apps. This pattern gives developers a direct, copy-pasteable custom view that avoids the gesture fights and coordinate bugs that make nested scroll containers feel broken.
Nesting two scrollable containers and getting the inner one to scroll first — then smoothly handing control to the outer container at the top or bottom — requires overriding `dispatchTouchEvent`, not `onTouchEvent`. The inner view must claim events on `ACTION_DOWN`, check `canScrollVertically` on `ACTION_MOVE`, and release its hold only when it hits a boundary. Using `ev.rawY` instead of `ev.y` prevents coordinate jumps that cause the two containers to fight over the gesture.
A common mistake is placing the logic in `onTouchEvent`. ScrollView's internal interception will send `ACTION_CANCEL` to a child that consumed `ACTION_DOWN` once the finger moves past the touch slop, so `onTouchEvent` never sees the move events. Moving everything into `dispatchTouchEvent` guarantees the inner view can intercept before the parent does.
A complete `NestedLogScrollView` implementation is provided, along with a demo Activity that appends log lines into the inner scroll area while the outer ScrollView holds surrounding content. The approach works for any `ScrollView` subclass and avoids the jitter that comes from misusing relative coordinates.
ScrollView's internal interception mechanism makes `onTouchEvent` a trap for nested scrolling: the framework will forcibly steal events from a child that consumed the down event, so any logic placed there is bypassed.
Using absolute screen coordinates (`rawY`) is the critical detail that prevents the inner and outer containers from oscillating — relative coordinates change as the container scrolls, creating feedback loops in the gesture decision.
The `canScrollVertically` check with a `-1` offset for boundary tolerance is a small but necessary guard against off-by-one errors that would otherwise block the handoff to the outer container.