saket
10/21/2021, 4:38 PMLazyColumn
with AndroidView
items and I’m just noticing that AndroidView#update
is getting called on every scroll/recomposition. Is this expected?tad
10/22/2021, 2:04 AMitem(key) { ... }
or item { key(...) { ... } }
to avoid unnecessary recompositions.Andrey Kulikov
10/22/2021, 1:22 PMsaket
10/22/2021, 3:23 PMLazyListState
. Because the background is synced with scroll, it’s causing recompositions on every scroll. What I don’t understand is why would my LazyColumn items also get invalidated if they aren’t reading this state. Here’s a gist of what my code looks like:
@Composable
fun Content() {
val listState = rememberLazyListState()
val scrollOffsetPx = listState.rememberScrollOffsetPx()
GradientBackground(scrollOffsetPx)
LazyColumn(state = listState) {
Row(...)
Row(...)
Row(...)
}
}
fun <V : View, M> LazyListScope.Row(
key: InvestmentEntityViewType, // Some enum
modifier: Modifier = Modifier,
model: M?,
create: () -> V,
render: V.(M) -> Unit = {}
) {
item(key) {
if (model != null) {
AndroidView(
modifier = modifier,
factory = { create() },
update = { view -> view.render(model) }
)
}
}
}
saket
10/22/2021, 4:14 PMAndrey Kulikov
10/22/2021, 4:27 PMsaket
10/22/2021, 5:04 PMContent
can be changed to localize the recomposition?Andrey Kulikov
10/22/2021, 5:27 PMsaket
10/22/2021, 6:06 PM@Composable
fun Content() {
val scrollConnection = remember {
object : NestedScrollConnection {
var scrollOffset by mutableStateOf(0f)
override fun onPostScroll(consumed: Offset, available: Offset, source: NestedScrollSource): Offset {
scrollOffset += consumed.y
return super.onPostScroll(consumed, available, source)
}
}
}
GradientBackground(yOffsetPx = scrollConnection.scrollOffset.roundToInt())
LazyColumn(modifier = Modifier.nestedScroll(scrollConnection)) {
...
}
}
saket
10/22/2021, 8:15 PMGradientDrawable
so that it can track scrolled pixelssaket
10/22/2021, 8:18 PMAndrey Kulikov
10/23/2021, 12:14 PM