How to do single choice in jetpack compose list
To do a single choice list in jetpack compose,
you have to think that you need a variable:
1 | val selectedOption = mutableStateOf( 0 ) |
And then in your item list you need to check the variable with the current item.
1 | RadioButton(selected = (text == selectedValue), |
When user click the item you need to save the new value
1 | onClick = {selectedOption.value = selected}) |
Your item list can be like this
1 2 | RadioButton(selected = (text == selectedValue), onClick = {selectedOption.value = selected}) |
You can check the full example in Github
or in here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import android.annotation.SuppressLint import android.content.Context import android.widget.Toast import androidx.compose.foundation.layout.* import androidx.compose.foundation. lazy .LazyColumn import androidx.compose.foundation. lazy .itemsIndexed import androidx.compose.foundation.selection.selectable import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.a.jetpackcomposelists.R @ Composable fun SingleScreen( viewModel : SingleViewModel, context : Context ) { Column(modifier = Modifier.fillMaxHeight()) { NameList(viewModel, context, Modifier.weight( 1 f)) } } @ SuppressLint( "UnrememberedMutableState" ) @ Composable fun NameList( viewModel : SingleViewModel, context : Context, modifier : Modifier ) { val selectedOption = mutableStateOf( 0 ) val optionsList = viewModel.getInitial(context) Scaffold(modifier = Modifier.fillMaxWidth(), topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app _ name)) }, actions = {}) }) { LazyColumn(modifier = modifier.fillMaxSize()) { itemsIndexed(items = optionsList) { selected : Int, item : String -> ElementSelected(text = item, selectedValue = optionsList[selectedOption.value], onClickListener = { selectedOption.value = selected Toast.makeText(context, item, Toast.LENGTH _ SHORT).show() }) TabRowDefaults.Divider(color = Color.LightGray) } } } } @ Composable fun ElementSelected( text : String, selectedValue : String, onClickListener : (String) -> Unit ) { Box( Modifier .height( 90 .dp) .fillMaxWidth() .selectable( selected = (text == selectedValue), onClick = { onClickListener(text) }) ) { RadioButton( modifier = Modifier .align(Alignment.CenterStart) .padding( 10 .dp), selected = (text == selectedValue), onClick = { onClickListener(text) }) Text( text = text, fontSize = 30 .sp, fontWeight = FontWeight.Bold, modifier = Modifier .padding(start = 16 .dp) .align(Alignment.Center) ) } } |
to choice a only choice in compose list
You can check the full example in Github