Ravi
07/16/2021, 11:30 AMval moduleItems: SnapshotStateList<TestModuleItem> = mutableStateListOf<TestModuleItem>().apply {
repeat(15) {
add(
TestModuleItem(
title = "Item: $it",
selected = false
)
)
}
}
@Composable
fun TestRender() {
Scaffold(
topBar = {
Text(text = "Test Application")
}
) {
TestList(
items = moduleItems,
onSelected = { index, flag ->
val item = moduleItems[index]
moduleItems[index] = item.copy(selected = flag)
}
)
}
}
@Composable
fun TestList(
modifier: Modifier = Modifier,
items: List<TestModuleItem>,
onSelected: (index: Int, selected: Boolean) -> Unit
) {
LazyColumn(modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.SpaceAround) {
itemsIndexed(items) { index, item ->
TestCompose(index, item, onSelected)
}
}
}
@Composable
fun TestCompose(
index: Int,
item: TestModuleItem,
onSelected: (index: Int, selected: Boolean) -> Unit
) {
println("current item, ${item.title}")
Row(
modifier = Modifier.fillMaxWidth()
) {
Text(text = "post ${item.title}")
Checkbox(checked = item.selected, onCheckedChange = {
onSelected(index, it)
})
}
}
data class TestModuleItem(
val title: String,
val selected: Boolean
)
TestModuleItem
is present in different module, on checkbox change all the items are getting recomposed. current item
is getting printed for all items when I toggle any checkboxTestModuleItem
is present
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
2. instead of passing TestModuleItem
in TestCompose
pass its properties, TestCompose(index, item.title, item.selected, onSelected)
TestModuleItem
is present in the same module as compose codemiqbaldc
07/17/2021, 5:26 PM