Can't make Jetpack Compose to recompose

811 Views Asked by At

I constructed a lazycolumn where on every row I set pointer listeners. What I want is that after long click on any row, the following clicks will be in selection mode and change the background color of the row. The long click works as intended, but the clicks afterwards, though receiving the events and setting associated values correctly, don't seem to make the UI refresh (or recompose), and so the background colors are not set. Any ideas please?

var selectedIndex by remember { mutableStateOf(-1) }

LazyColumn {
    itemsIndexed(lazyPagingItems) { index, row ->
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .pointerInput(Unit) {
                    detectTapGestures(
                        onLongPress = {
                            onLongClickAction(row, index)
                            selectedIndex++
                        },
                        onTap = {
                            println("clicked $index $selectMode")
                            if (!selectMode) {                                  
                                println("$index " + colorSelected(index))
                            } else {
                                selectMap[index] = row
                            }
                            selectedIndex++
                            println("$index " + colorSelected(index))
                        }
                    )
                }
                .background(colorResource(colorSelected(index))),

            horizontalArrangement = Arrangement.SpaceBetween
        ) {
             Text(
                text = index.toString(),
                modifier = Modifier.padding(end = 2.dp)
            )
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

You are not reading var selectedIndex by remember { mutableStateOf(-1) } anywhere in your code what triggers a recomposition in any Composable reading a mutableState value change. You can check here for smart recomposition.