admin管理员组文章数量:1313800
I have created an app and added a component which is a drop down list. It use to work but I cannot make it work again. When I click on the item, the click is not caught.
The code is as below:
@Composable
fun SortedBySelector(viewModel: MainViewModel) {
var expanded by remember { mutableStateOf(false) }
var selectedType by remember { mutableStateOf(SortedBy.BY_DATE) }
Box(
modifier = Modifier
) {
Row(modifier = Modifier
.clickable { expanded = !expanded }
) {
Text(selectedType.value)
if(expanded) {
Icon(Icons.Default.KeyboardArrowUp, contentDescription = "Arrow Up")
} else {
Icon(Icons.Default.KeyboardArrowDown, contentDescription = "Arrow Down")
}
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
containerColor = MaterialTheme.colorScheme.background
) {
listOfSortedBy.forEach {
DropdownMenuItem(
text = { Text(it.value) },
onClick = {
expanded = !expanded
selectedType = it
viewModel.refreshCategory(it)
}
)
}
}
}
}
Any idea why ? I cannot figure-out. The issue is on the emulator.
I have created an app and added a component which is a drop down list. It use to work but I cannot make it work again. When I click on the item, the click is not caught.
The code is as below:
@Composable
fun SortedBySelector(viewModel: MainViewModel) {
var expanded by remember { mutableStateOf(false) }
var selectedType by remember { mutableStateOf(SortedBy.BY_DATE) }
Box(
modifier = Modifier
) {
Row(modifier = Modifier
.clickable { expanded = !expanded }
) {
Text(selectedType.value)
if(expanded) {
Icon(Icons.Default.KeyboardArrowUp, contentDescription = "Arrow Up")
} else {
Icon(Icons.Default.KeyboardArrowDown, contentDescription = "Arrow Down")
}
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
containerColor = MaterialTheme.colorScheme.background
) {
listOfSortedBy.forEach {
DropdownMenuItem(
text = { Text(it.value) },
onClick = {
expanded = !expanded
selectedType = it
viewModel.refreshCategory(it)
}
)
}
}
}
}
Any idea why ? I cannot figure-out. The issue is on the emulator.
Share Improve this question asked Jan 30 at 15:27 SebSeb 3,2255 gold badges39 silver badges87 bronze badges 1- I tried to create a minimal reproducible example by removing all your ViewModel related code and adding the listOfSortedBy variable, but I can't reproduce the issue. The DropDownMenu correctly gets closed after an item is selected. – BenjyTec Commented Jan 31 at 5:36
1 Answer
Reset to default 0Try this it works Fine
val context = LocalContext.current
val menuItems = listOf("Image", "PDF File")
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 10.dp),
) {
Text(
text = "Select Media",
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onPrimary,
)
Box {
TextButton(
onClick = { expanded = !expanded },
contentPadding = PaddingValues(0.dp),
) {
Icon(
imageVector = Icons.Default.AddCircle,
contentDescription = "Add Circle",
tint = MaterialTheme.colorScheme.primary,
)
Text(
text = stringResource(id = R.string.add),
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.padding(start = 5.dp)
)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = !expanded },
modifier = Modifier.background(color = Dark_Gunmetal),
) {
menuItems.forEachIndexed { index, menuItem ->
DropdownMenuItem(
onClick = {
expanded = false
when (index) {
0 -> context.showToast("Image Clicked")
1 -> context.showToast("PDF Clicked")
}
},
leadingIcon = {
Icon(
painter = painterResource(if (menuItem == "Image") R.drawable.baseline_person_24 else R.drawable.ic_term),
contentDescription = menuItem,
tint = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.size(18.dp)
)
},
text = {
Text(
text = menuItem,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onPrimary
)
}
)
if (menuItem != menuItems.last()) HorizontalDivider(
color = MaterialTheme.colorScheme.onSecondary
)
}
}
}
}
本文标签: androidDropDown menu not clickable when expandedStack Overflow
版权声明:本文标题:android - DropDown menu not clickable when expanded - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741955690a2406976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论