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

1 Answer 1

Reset to default 0

Try 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