I have a parent composable that is scrollable and contains a child AndroidView. Inside the AndroidView, there is a NestedScrollView. I expected the child NestedScrollView to be able to scroll before the parent composable, but in reality, it is not able to scroll. Here is my code:
@Composable
fun TestScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
) {
AndroidView(
factory = {
NestedScrollView(it).apply {
ViewCompat.setNestedScrollingEnabled(this, true)
layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
addView(TextView(it).also { tv ->
tv.layoutParams = ViewGroup.MarginLayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
tv.text = "Inner scrollable"
tv.textSize = 100f
tv.setBackgroundColor(Color.Blue.toArgb())
})
}
}, modifier = Modifier
.fillMaxWidth()
.height(200.dp)
.clipToBounds()
)
Divider(color = Color.Red)
Text(
text = "Outer scrollable", fontSize = 50.sp, modifier = Modifier
.fillMaxWidth()
.height(800.dp)
)
Divider(color = Color.Red)
}
}
I understand that I can add verticalScroll to the AndroidView, but that doesn't meet my expectations because it will make the NestedScrollView larger than the AndroidView. Currently, it's not an issue, but if I were to use a RecyclerView instead of a NestedScrollView, the items within the NestedScrollView wouldn't be recyclable, which would be a significant problem.
As i can see,The
NestedScrollViewyou've created as anAndroidViewis not properly integrated with Jetpack Compose'sscrollingsystem. You should use Jetpack Compose's ownLazyColumnorVerticalScrollbarto achieve the desired nested scroll behavior.Follow the code:
Logic:
We are using
LazyColumnto create ascrollablearea for theinner content. The outer and inner content is organized within aColumn, and Jetpack Compose's scrolling system handles thenested scrollingbehavior automatically. This will give you the desirednested scrollfunctionality.