In an Android View screen, a Compose LazyColumn inside a CoordinatorLayout scrolls but never collapses the toolbar; why, and how does rememberNestedScrollInteropConnection fix it?
answer
- two nested-scroll systems
- the View parent never hears Compose
- a connection that speaks View nested scrolling
- Modifier.nestedScroll on the scrollable
- parent must implement NestedScrollingParent3
basics
~10 sThe LazyColumn's scroll deltas travel only through Compose's nested-scroll chain, so CoordinatorLayout never receives them. Applying Modifier.nestedScroll(rememberNestedScrollInteropConnection()) bridges those deltas to the cooperating View parent, which collapses the toolbar.
solid answer
~40 sCompose and Views each have their own nested-scroll protocol. A `LazyColumn` dispatches scroll and fling deltas to Compose ancestors, but `CoordinatorLayout` receives nested scrolls through the View protocol, from a child View that dispatches them, so the `AppBarLayout` behaviour never sees the list's scroll. `rememberNestedScrollInteropConnection()` returns a `NestedScrollConnection` that bridges the two: it enables nested scrolling on the host view, `LocalView.current` by default, and forwards pre-scroll, post-scroll and fling deltas to a View parent that implements `NestedScrollingParent3`, such as `CoordinatorLayout`. Attach it with `Modifier.nestedScroll(connection)` on the scrollable or an ancestor inside the `ComposeView`. It covers only cooperating parents. The reverse direction, a scrolling View inside Compose, works through `AndroidView` when that View has nested scrolling enabled.
code
kotlin · 10 linesbinding.roomList.setContent {
KaraokeTheme {
val connection = rememberNestedScrollInteropConnection()
LazyColumn(modifier = Modifier.nestedScroll(connection)) {
items(rooms) { room ->
RoomRow(room)
}
}
}
}go deeper
Know that Compose and Views scroll-coordinate separately and that rememberNestedScrollInteropConnection joins a Compose list to a CoordinatorLayout.
Explain where Modifier.nestedScroll with the connection goes and why the parent must implement NestedScrollingParent3.
Diagnose a toolbar that never collapses, flings that do not propagate, and View-in-Compose scrolling that stays silent, in both directions across the seam.
Weigh bridging nested scroll on a mixed screen against migrating the whole CoordinatorLayout screen to Compose, given the coordination code each choice keeps alive.
## The setup A karaoke-booking screen is still a View layout: a `CoordinatorLayout` with an `AppBarLayout` whose toolbar should collapse as the user scrolls a list of rooms. The list has been rewritten as a Compose `LazyColumn` inside a `ComposeView` that carries the app-bar scrolling behaviour in its layout params. The list scrolls, but the toolbar never moves. ## Why nothing reaches the toolbar **Nested scrolling** lets a parent react to a child's scroll: collapse a header before the list moves, or take the leftover delta when the list hits its end. Views and Compose implement it separately: | | View system | Compose | |---|---|---| | Child side | `NestedScrollingChild3` dispatches deltas | scrollables dispatch through a `NestedScrollDispatcher` | | Parent side | `NestedScrollingParent3`, for example `CoordinatorLayout` | `Modifier.nestedScroll(connection)` with a `NestedScrollConnection` | | Units | integer pixels | float pixels | A `LazyColumn` dispatches only into the Compose chain. Without a bridge, its deltas end at the root of the composition and the `CoordinatorLayout` behaviours never receive them. ## The fix: rememberNestedScrollInteropConnection `rememberNestedScrollInteropConnection(hostView: View = LocalView.current)` creates and remembers a `NestedScrollConnection` that enables **nested scroll interop between a View parent implementing `NestedScrollingParent3` and a Compose child**. Its KDoc describes it as acting on the View side like a nested-scrolling child, dispatching deltas up to a consuming parent, while receiving deltas from dispatching children on the Compose side. When created it turns on nested scrolling for the host view. To wire it: 1. Inside the `ComposeView`'s content, call `val connection = rememberNestedScrollInteropConnection()`. 2. Apply `Modifier.nestedScroll(connection)` to the scrollable or one of its ancestors in the composition. 3. Keep the View-side layout configured as usual: the `ComposeView` is the direct child of the `CoordinatorLayout` with the app-bar scrolling behaviour, as a `RecyclerView` would be. From then on, the deltas flow like this on every drag: 1. The user drags the `LazyColumn`; before it consumes anything, Compose's nested-scroll chain offers the delta to its ancestors as a **pre-scroll**. 2. The interop connection forwards that pre-scroll to the View parent; `CoordinatorLayout` lets the `AppBarLayout` behaviour consume part of it to collapse the toolbar, and reports what it consumed. 3. The list scrolls by whatever is left. 4. Any delta the list could not use, for example at the end of its content, goes up as a **post-scroll**, which the View parent can also consume. 5. When the finger lifts, the fling velocity is offered the same way, before and after the list flings. The KDoc is explicit about the limit: it covers **only a cooperating parent**, one that implements `NestedScrollingParent3`, and names `CoordinatorLayout` as the key example. A parent outside that protocol, such as a plain `LinearLayout`, has nothing to cooperate with. ## The reverse direction: a View scrolling inside Compose Sometimes the seam is the other way round: a legacy scrolling View hosted by `AndroidView` inside a Compose screen whose top app bar should collapse. `AndroidView`'s holder acts as a View nested-scrolling **parent** for the hosted View and dispatches what it receives into the Compose chain, but only **if the hosted View has nested scrolling enabled**. Call `setNestedScrollingEnabled(true)` on the View in `factory` when it is not already enabled, and the Compose ancestor with `Modifier.nestedScroll` receives the deltas. ## Diagnosing a broken seam - Toolbar never moves: no interop connection on the Compose scrollable, or the `ComposeView` lacks the app-bar scrolling behaviour in the `CoordinatorLayout`. - Connection applied to a different composable than the list: it must sit on the scrollable or one of its ancestors, or the list's deltas never pass through it. - View inside Compose does not drive a Compose header: the hosted View has nested scrolling disabled. - Parent is not a `NestedScrollingParent3`: the connection has nothing to talk to; restructure the parent or migrate the whole screen. ## Summary - Two protocols, no automatic bridge from a Compose scrollable to a View parent. - `Modifier.nestedScroll(rememberNestedScrollInteropConnection())` is the bridge for Compose inside a cooperating View parent. - `AndroidView` bridges the other way when the hosted View has nested scrolling enabled.
- How do you make a scrolling View hosted with AndroidView drive a collapsing header written in Compose?`AndroidView` acts as a View nested-scrolling parent for its child and dispatches what it receives into Compose's nested-scroll chain, but only when the hosted View has nested scrolling enabled. Call `setNestedScrollingEnabled(true)` on it in `factory` if needed, and give the Compose ancestor that owns the header a `Modifier.nestedScroll` connection.
- Why does rememberNestedScrollInteropConnection not help when the ComposeView's parent is a plain LinearLayout?Its documentation limits it to cooperating parents that implement `NestedScrollingParent3`, with `CoordinatorLayout` as the key example. A `LinearLayout` does not take part in the View nested-scrolling protocol, so nothing consumes the forwarded deltas and the bridge has nothing to cooperate with.
- ComposeView forwards scroll deltas to CoordinatorLayout automatically
- The connection belongs on the CoordinatorLayout rather than inside Compose
- Any ViewGroup parent cooperates with the interop connection, LinearLayout included
- AndroidView forwards nested scroll even when its View has nested scrolling disabled
- Nested scroll interop only works from View to Compose