android-jetpack-composeclickable

Is there a way to get index of clickable in jetpack compose?


So i checked out this video https://www.youtube.com/watch?v=1Thp0bB5Ev0&t=73s and i get the Lazycolumn exchange from recyclerview though Philip as he is called uses a column first and says if you do not need a huge list just use column. My question is is it possible to use only column and get the clicked index of the list? my code looks like this.

Column() {
    viewModel.settings.forEach { setting ->
        Column(modifies = Modifier.clickable{//can i check thje index here somehow??}) {
            Text(text = setting.name, textAlign = TextAlign.Left, fontSize = 24.sp, fontWeight = FontWeight.Bold)
            Text(text = setting.description, textAlign = TextAlign.Left, fontSize = 12.sp)
        }
    }
}

Solution

  • There is a forEachIndexed in Kotlin you can use:

    Column() {
    viewModel.settings.forEachIndexed { index, setting ->
        Column(modifies = Modifier.clickable{ use the index }) {
            Text(text = setting.name, textAlign = TextAlign.Left, fontSize = 24.sp, fontWeight = FontWeight.Bold)
            Text(text = setting.description, textAlign = TextAlign.Left, fontSize = 12.sp)
            }
        }
    }