Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading