karn
04/02/2022, 12:11 AM// Use recomposeHighlighter from:
// <https://github.com/android/snippets/blob/master/compose/recomposehighlighter/src/main/java/com/example/android/compose/recomposehighlighter/RecomposeHighlighter.kt>
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
val mutableState = remember {
mutableStateOf(listOf(1, 2, 3))
}
LazyColumn(
state = rememberLazyListState(),
modifier = Modifier.fillMaxWidth()
) {
itemsIndexed(
items = mutableState.value,
key = { index: Int, item: Int -> item }
) { index, item ->
Text(modifier = Modifier
.recomposeHighlighter()
.clickable {
mutableState.value = mutableState.value + Random.nextInt(3, 100000)
}, text = "$item")
}
}
}
}
}
}
myanmarking
04/02/2022, 12:52 AMDan Yang
04/03/2022, 7:35 AMmutableState
if it's used to back the `lazyColumn`'s collection.
The closest I got was extracting the mustableState
reference in the clickable
but it still causes the whole list to recompose:
val mutableState = remember {
mutableStateOf(listOf(1, 2, 3))
}
val textClickable = remember {
{
mutableState.value =
mutableState.value + Random.nextInt(3, 100000)
}
}
LazyColumn(
state = rememberLazyListState(),
modifier = Modifier.fillMaxWidth()
) {
itemsIndexed(
items = mutableState.value,
key = { index: Int, item: Int -> item }
) { index, item ->
Text(modifier = Modifier
.recomposeHighlighter()
.clickable(onClick = textClickable), text = "$item"
)
}
}
myanmarking
04/03/2022, 5:32 PMvar mutableState: List<Int> by remember {
mutableStateOf(
listOf(1, 2, 3)
)
}
val onClick = {
mutableState = mutableState + Random.nextInt(3, 100000)
}
LazyColumn(
state = rememberLazyListState(),
modifier = Modifier.fillMaxWidth()
) {
itemsIndexed(
items = mutableState,
key = { _: Int, item: Int -> item }
) { _, item ->
MyText(
label = item.toString(),
onClick = onClick
)
}
}
@Composable
private fun MyText(
label: String,
onClick: () -> Unit
) {
Text(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 5.dp)
.recomposeHighlighter()
.clickable(onClick = onClick),
text = label
)
}
Dan Yang
04/04/2022, 4:08 PMskippable
composables. From https://github.com/androidx/androidx/blob/androidx-main/compose/compiler/design/compiler-metrics.md#functions-that-are-restartable-but-not-skippable:
Skippability means that when called during recomposition, compose is able to skip the function if all of the parameters are equal.When split out,
MyText
is skippable
but perhaps LazyColumn
is not skippable (maybe because of the List<*>
) therefore the whole function body is run.
I tried changing MyText
to take a non-stable param and it started recomposing when I clicked on the text again.
data class Data(val item: Int)
@Composable
fun SpecialText(item: Data, textClickable: () -> Unit) {
Text(modifier = Modifier
.recomposeHighlighter()
.clickable(onClick = textClickable), text = "$item"
)
}