Conexões de Rolagem Aninhada
Coordene a rolagem entre contêineres.
Conexões de Rolagem Aninhada é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Scrolls Inside Scrolls
Sometimes a scrollable sits inside another scrollable, like a list under a collapsing header. Nested scroll lets them cooperate instead of fighting. 📜
The Coordination Problem
Without help, the inner list eats every swipe and the outer container never moves. NestedScrollConnection shares the scroll between them.
Meet nestedScroll
Attach the nestedScroll modifier and pass a connection. That connection intercepts scroll deltas as they travel up the tree.
Modifier.nestedScroll(connection)Pre-Scroll: First Dibs
The parent gets a chance before the child via onPreScroll. Return how much of the delta you consumed; the rest flows to the child.
override fun onPreScroll(available: Offset, source): OffsetPost-Scroll: Leftovers
After the child scrolls, onPostScroll offers any unused delta back to the parent. Use it to move a header once the list hits its edge.
override fun onPostScroll(consumed, available, source): OffsetConsume What You Need
In each callback you return an Offset of how much you used. Return Offset.Zero to pass everything through to the next participant.
return Offset(0f, consumedY)Fling Phases Too
There are matching onPreFling and onPostFling callbacks for momentum. They are suspend functions so you can animate as the fling resolves.
override suspend fun onPreFling(available: Velocity): VelocityRemember the Connection
Wrap your connection in remember so it survives recomposition. Capture any state it reads, like a header offset, inside it.
val connection = remember { object : NestedScrollConnection {...} }Collapsing Toolbar Classic
The flagship use is a collapsing toolbar: shrink the top bar with pre-scroll while the list scrolls, then expand it on the way back.
It Just Works Often
Many built-ins like Scaffold with scroll behaviors already wire nested scroll for you. Reach for a custom connection only when you need fine control.
Mind the Source
Each callback gets a source telling you if the scroll came from a drag or a fling. Use it to react differently to touch versus momentum.
if (source == NestedScrollSource.Drag) { /* ... */ }Quick Check
Which callback lets a parent consume scroll before its child does?
Recap: Scrolls in Harmony
You learned NestedScrollConnection with pre and post scroll and fling, the key to collapsing toolbars and cooperating lists. 🎉
Aprenda Kotlin com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 30
- Aulas
- 120
Perguntas Frequentes
A aula “Conexões de Rolagem Aninhada” é grátis?
Sim — o texto completo de “Conexões de Rolagem Aninhada” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.
O que vou aprender em “Conexões de Rolagem Aninhada”?
Coordene a rolagem entre contêineres. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Jetpack Compose Academy?
Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Conexões de Rolagem Aninhada”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Jetpack Compose Academy?
Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- pointerInput e detectTapGestures
- Arrastar, Deslizar e Arremessar
- Transformar: Deslocar, Ampliar e Girar
- Conexões de Rolagem Aninhada