Modifier.nestedScroll(connection, dispatcher?):将组件附加到嵌套滚动链上。NestedScrollConnection: 重写onPreScroll/onPostScroll方法,以在子级处理滚动前后消耗或响应滚动偏移量。NestedScrollDispatcher: 在构建自定义可滚动组件时,向父级(上游)派发滚动/抛掷事件。
1 | val nestedConnection = remember { |
This connection lets you consume or react to scroll deltas before and after the child handles them.
After that link your connection to a parent container using Modifier.nestedScroll, so it joins the nested‑scroll chain.
1 | Box( |
Inside onPreScroll, adjust a header’s height state and return how much you consumed. For example:
1 | val toolbarHeight = remember { mutableStateOf(maxHeightDp) } |
This ensures the header collapses as you scroll up and expands on scroll down.
Wrap a LazyColumn and your header in a Box with Modifier.nestedScroll(connection). Tie the header’s height and offset modifiers to the state updated in onPreScroll:
1 |
|