abu naser
10/18/2022, 5:18 AM@Composable
fun ProblemOfLazyClickable() {
var counter by remember { mutableStateOf(0) }
val dummyItemList = remember {
mutableStateListOf("dummy init")
}
Column(modifier = Modifier.fillMaxSize()) {
Button(onClick = { dummyItemList.add("dummy item $counter") ; counter++} ) {
Text(text = "Add Dummy")
}
Button(onClick = { dummyItemList.remove(dummyItemList.random()) }) {
Text(text = "Remove random Dummy")
}
LazyVerticalGrid(columns = GridCells.Adaptive(minSize = 100.dp), content = {
items(dummyItemList){ dummy ->
DummyCard(item = dummy, onClick ={
Log.d("dummyEvent","Clicked $it")
}, onLongClick = {
Log.d("dummyEvent","Long Clicked $it")
} )
}
})
}
}
@Composable
fun DummyCard(item:String,onClick:(String)->Unit ,onLongClick:(String)->Unit) {
Card(modifier = Modifier
.pointerInput(Unit){
detectTapGestures(
onPress = { onClick(item) },
onLongPress = { onLongClick(item) }
)
}
.padding(4.dp)
.sizeIn(100.dp, 200.dp, 100.dp, 200.dp)
//clickable works fine
// .clickable { onClick(item) }
, backgroundColor = Color.LightGray
) {
Text(item)
}
}
Stylianos Gakis
10/18/2022, 8:43 AMitems
? In your case it’s just strings so maybe there’s no unique key there, but maybe if you use itemsIndexed and use the index as the unique key, like
LazyVerticalGrid(...) {
itemsIndexed(
asd,
{ index, _ -> index }
) {
DummyCard(...)
}
}
In general, if you don’t give a key to items on a list, you’re not guaranteed that things will not carry over to other calls when you add/remove items from composition just like that.abu naser
10/18/2022, 9:14 AMStylianos Gakis
10/18/2022, 9:58 AMabu naser
10/18/2022, 10:21 AM@Composable
fun ProblemOfLazyClickable() {
var counter by remember { mutableStateOf(1) }
val dummyItemList = remember {
mutableStateListOf("dummy init")
}
val dummyItemList2 = remember {
mutableStateListOf(Test(0,"init item"))
}
Column(modifier = Modifier.fillMaxSize()) {
Button(onClick = {
dummyItemList.add("dummy item $counter")
dummyItemList2.add(Test(counter,"dummy item $counter"))
counter++
}) {
Text(text = "Add Dummy")
}
Button(onClick = {
dummyItemList.remove(dummyItemList.random())
dummyItemList2.remove(dummyItemList2.random())
}) {
Text(text = "Remove random Dummy")
}
LazyVerticalGrid(columns = GridCells.Adaptive(minSize = 100.dp)) {
itemsIndexed(dummyItemList2, { index, _ ->
index
}, contentType = { _: Int, item: Test -> item }){index: Int, item: Test ->
DummyCard2(item = item, onClick = {
Log.d("dummyEvent","Clicked $it")
}, onLongClick = {
Log.d("dummyEvent","Long Clicked $it")
})
}
}
}
}
data class Test(
val id:Int,
val text:String
)
@Composable
fun DummyCard2(item:Test,onClick:(Test)->Unit ,onLongClick:(Test)->Unit) {
Card(modifier = Modifier
.pointerInput(Unit) {
detectTapGestures(
onLongPress = { onClick(item) },
onPress = { onLongClick(item) }
)
}
.padding(4.dp)
.sizeIn(100.dp, 200.dp, 100.dp, 200.dp)
//clickable works fine
// .clickable { onClick(item) }
, backgroundColor = Color.LightGray
) {
Text(item.text)
}
}
Stylianos Gakis
10/18/2022, 11:16 AM{ index, _ -> index }
as the key, go back to just items
instead of the indexed variant, and do { item -> item.id }
as the key. So the items themselves will retain their ID and that will be the unique identifier to that itemabu naser
10/18/2022, 12:20 PMStylianos Gakis
10/18/2022, 12:55 PM