I want to show a ExposedDropdownMenu which list is coming from api. Is it possible to add pagination in ExposedDropdownMenu. Here I tried the code
DivisionDropDown.kt
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DivisionDropDown(
list: List<String>,
selectedItem: String,
itemSelectListener: (String) -> Unit,
modifier: Modifier = Modifier
) {
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded },
modifier = modifier.then(
Modifier.wrapContentWidth()
)
) {
OutlinedTextField(
value = selectedItem,
shape = RoundedCornerShape(12.dp),
label = {
Text(
text = stringResource(id = R.string.division_of_school)
)
},
readOnly = true,
onValueChange = { itemSelectListener(it) },
trailingIcon = {
Icon(
painter = painterResource(R.drawable.ic_down),
contentDescription = "",
modifier = Modifier.rotate(if (expanded) 180f else 0f)
)
},
maxLines = 1,
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.menuAnchor()
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.exposedDropdownSize(true),
) {
list.forEach {
DropdownMenuItem(
text = { Text(it) },
onClick = {
itemSelectListener(it)
expanded = false
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
)
}
}
}
}
here I used List to show it in the dropdown menu. And the ScreenUI code is given bellow
SchoolInfoScreen.kt
@Composable
fun SchoolInfoScreen(
viewModel: ProfileEditViewModel
) {
val divisionPagingItems: LazyPagingItems<Division> = viewModel.divisionState.collectAsLazyPagingItems()
ConstraintLayout(
Modifier
.fillMaxWidth()
.fillMaxHeight()
.padding(horizontal = 16.dp)
) {
val (divisionDropDown) = createRefs()
DivisionDropDown(
list = divisionList, // Here how can I use divisionPagingItems
selectedItem = selectedDivision,
itemSelectListener = {
},
modifier = Modifier
.constrainAs(divisionDropDown) {
top.linkTo(parent.top, 32.dp)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
)
}
}
Finally I solved the problem. Here is the solution.