Skip to content

Instantly share code, notes, and snippets.

@BlondeNamazu
Created March 4, 2023 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlondeNamazu/5e670bb04c65da36a3d9489d74cc9efe to your computer and use it in GitHub Desktop.
Save BlondeNamazu/5e670bb04c65da36a3d9489d74cc9efe to your computer and use it in GitHub Desktop.
ブログにて紹介したNestedScrollの挙動をカスタマイズする際に用いたサンプルコード https://blondenamazu.hatenablog.com/entry/manage-nested-scroll
package com.example.nestedScrollSample
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color.Companion.Cyan
import androidx.compose.ui.graphics.Color.Companion.Red
import androidx.compose.ui.graphics.Color.Companion.Yellow
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@Composable
fun ChildScrolledContent(
modifier: Modifier = Modifier,
) {
LazyColumn(
modifier = modifier
.nestedScroll(
connection = object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset {
return available
}
}
)
.height(150.dp),
) {
items(10) { index ->
Column {
Text(
modifier = Modifier.fillMaxWidth(),
text = "item $index",
textAlign = TextAlign.Center,
)
Spacer(modifier = Modifier.height(4.dp))
}
}
}
}
@Composable
fun ParentScrolledContent(
modifier: Modifier = Modifier
) {
LazyColumn(
modifier = modifier
.width(200.dp)
.height(350.dp)
) {
item {
ChildScrolledContent(
modifier = Modifier
.background(color = Yellow)
)
}
item {
ChildScrolledContent(
modifier = Modifier
.background(color = Red)
)
}
item {
ChildScrolledContent(
modifier = Modifier
.background(color = Cyan)
)
}
}
}
@Preview
@Composable
fun ParentScrolledContentPreview() {
ParentScrolledContent()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment