When is it advantageous to use NestedScrollDispatcher above NestedScrollConnection? Please list an example of NestedScrollDispatcher in action.
NestedScrollConnection and NestedScrollDispatcher are made for different use cases in the nested scroll system. There is no "advantage of one over the other" you have to use the one that fits your use case, generally both:
NestedScrollDispatcher: Use if you are a scrollable composeable, to communicate your received scroll events to parent, asking them if they first want to consume some of your scroll/fling, then scrolling/flinging yourself and then reporting to your parent what's left of the scroll/fling after you're done (e.g. if you are a list already scrolled to its end)NestedScrollConnection: Use if you are a parent of a scrollable child that wants to react to scroll events of the child (e.g. an LazyList with a scrollable item or a layout with a LazyList and a collapsible header). You will get notified by children if you first want to consume any of their scroll/fling before they do and you will be given the scroll/fling left after the child is done.Note that the methods of the Dispatcher of children basically call the methods of the Connection of their parent (dispatchPreScroll() -> onPreScroll() etc.)
To participate in the nested scrolling hierarchy use the nestedScroll modifier which takes both a NestedScrollConnection and NestedScrollDispatcher. You are, however, allowed to only watch/consume if you are a parent that is not scrollable by itself and only wants to react to child scrolls (which is why the dispatcher argument is optional.
In most cases you don't need to worry about it, because LazyList, Pager, Modifier.scrollable etc. already implement the nestedScroll modifier. But if you want to customize the behavior you can e.g. pass your own NestedScrollConnection to a Pager. Also of you build a custom component that uses lower-level APIs to be scrollable, you will need to use the nestedScroll modifier.
The documentation elaborates this quite well imo and also provides an example of how to use NestedScrollDispatcher. There is also a Medium Article with a concrete use case of a NestedScrollConnection used to make a non-scrollable parent scrolled through its scrollable child.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With