Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In dieser Anleitung wird beschrieben, wie Sie in Compose eine dynamische obere App-Leiste erstellen, deren Optionen sich ändern, wenn Elemente aus der Liste ausgewählt werden. Sie können den Titel und die Aktionen der oberen App-Leiste basierend auf dem Auswahlstatus ändern.
Dynamisches Verhalten der oberen App-Leiste implementieren
Dieser Code definiert eine zusammensetzbare Funktion für die obere App-Leiste, die sich je nach Auswahl des Elements ändert:
@ComposablefunAppBarSelectionActions(selectedItems:Set<Int>,modifier:Modifier=Modifier,){valhasSelection=selectedItems.isNotEmpty()valtopBarText=if(hasSelection){"Selected ${selectedItems.size} items"}else{"List of items"}TopAppBar(title={Text(topBarText)},colors=TopAppBarDefaults.topAppBarColors(containerColor=MaterialTheme.colorScheme.primaryContainer,titleContentColor=MaterialTheme.colorScheme.primary,),actions={if(hasSelection){IconButton(onClick={/* click action */}){Icon(imageVector=Icons.Filled.Share,contentDescription="Share items")}}},)}
AppBarSelectionActions akzeptiert ein Set von ausgewählten Elementindexen.
Das topBarText ändert sich je nachdem, ob Sie Elemente auswählen.
Wenn Sie Elemente auswählen, wird im TopAppBar Text mit der Anzahl der ausgewählten Elemente angezeigt.
Wenn Sie keine Elemente auswählen, ist topBarText „Liste der Elemente“.
Mit dem Block actions werden die Aktionen definiert, die in der oberen App-Leiste angezeigt werden. Wenn hasSelection „true“ ist, wird nach dem Text ein Freigabesymbol angezeigt.
Die onClick-Lambda-Funktion von IconButton verarbeitet die Freigabeaktion, wenn Sie auf das Symbol klicken.
Ergebnis
Abbildung 1: Eine dynamische obere App-Leiste mit Text und einem Freigabesymbol, die nur angezeigt werden, wenn Elemente ausgewählt sind.
Auswählbare Liste in dynamische obere App-Leiste einbinden
In diesem Beispiel wird gezeigt, wie Sie einer dynamischen oberen App-Leiste eine auswählbare Liste hinzufügen:
Die obere Leiste wird entsprechend der Anzahl der ausgewählten Listenelemente aktualisiert.
selectedItems enthält die Gruppe der ausgewählten Elementindexe.
AppBarMultiSelectionExample verwendet eine Scaffold, um den Bildschirm zu strukturieren.
Mit topBar = { AppBarSelectionActions(selectedItems) } wird die AppBarSelectionActions-Composable als obere App-Leiste festgelegt.
AppBarSelectionActions erhält den Status selectedItems.
LazyColumn zeigt die Elemente in einer vertikalen Liste an, wobei nur die Elemente gerendert werden, die auf dem Bildschirm sichtbar sind.
ListItemSelectable steht für ein auswählbares Listenelement.
Mit combinedClickable können sowohl Klicks als auch lange Klicks für die Auswahl von Elementen verarbeitet werden. Bei einem Klick wird eine Aktion ausgeführt, während durch langes Klicken auf ein Element der Auswahlstatus umgeschaltet wird.
Ergebnis
Abbildung 2. Eine Liste, die in eine dynamische obere App-Leiste integriert ist, wobei bestimmte Elemente ausgewählt sind.
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-08-30 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-08-30 (UTC)."],[],[],null,["This guide explains how to create a dynamic top app bar in Compose that changes\nits options when items are selected from the list. You can modify the top app\nbar's title and actions based on the selection state.\n\nImplement dynamic top app bar behavior\n\nThis code defines a composable function for the top app bar that changes based\non item selection:\n\n\n```kotlin\n@Composable\nfun AppBarSelectionActions(\n selectedItems: Set\u003cInt\u003e,\n modifier: Modifier = Modifier,\n) {\n val hasSelection = selectedItems.isNotEmpty()\n val topBarText = if (hasSelection) {\n \"Selected ${selectedItems.size} items\"\n } else {\n \"List of items\"\n }\n\n TopAppBar(\n title = {\n Text(topBarText)\n },\n colors = TopAppBarDefaults.topAppBarColors(\n containerColor = MaterialTheme.colorScheme.primaryContainer,\n titleContentColor = MaterialTheme.colorScheme.primary,\n ),\n actions = {\n if (hasSelection) {\n IconButton(onClick = {\n /* click action */\n }) {\n Icon(\n imageVector = Icons.Filled.Share,\n contentDescription = \"Share items\"\n )\n }\n }\n },\n )\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/compose/snippets/src/main/java/com/example/compose/snippets/components/AppBar.kt#L402-L435\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- `AppBarSelectionActions` accepts a `Set` of selected item indexes.\n- The `topBarText` changes depending on whether you select any items.\n - When you select items, text describing the number of items selected appears in the [`TopAppBar`](/reference/kotlin/androidx/compose/material3/package-summary#TopAppBar(kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Function1,androidx.compose.ui.unit.Dp,androidx.compose.foundation.layout.WindowInsets,androidx.compose.material3.TopAppBarColors,androidx.compose.material3.TopAppBarScrollBehavior)).\n - If you don't select any items, the `topBarText` is \"List of items\".\n- The `actions` block defines the actions you display in the top app bar. If `hasSelection` is true, a share icon appears after the text.\n- The `onClick` lambda of the [`IconButton`](/reference/kotlin/androidx/compose/material3/package-summary#IconButton(kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.material3.IconButtonColors,androidx.compose.foundation.interaction.MutableInteractionSource,androidx.compose.ui.graphics.Shape,kotlin.Function0)) handles the share action when you click the icon.\n\nResult **Figure 1.** A dynamic top app bar with text and a share icon that only appear when items are selected.\n\nIntegrate selectable list into dynamic top app bar\n\nThis example demonstrates how to add a selectable list to a dynamic top app bar:\n\n\n```kotlin\n@Composable\nprivate fun AppBarMultiSelectionExample(\n modifier: Modifier = Modifier,\n) {\n val listItems by remember { mutableStateOf(listOf(1, 2, 3, 4, 5, 6)) }\n var selectedItems by rememberSaveable { mutableStateOf(setOf\u003cInt\u003e()) }\n\n Scaffold(\n topBar = { AppBarSelectionActions(selectedItems) }\n ) { innerPadding -\u003e\n LazyColumn(contentPadding = innerPadding) {\n itemsIndexed(listItems) { _, index -\u003e\n val isItemSelected = selectedItems.contains(index)\n ListItemSelectable(\n selected = isItemSelected,\n Modifier\n .combinedClickable(\n interactionSource = remember { MutableInteractionSource() },\n indication = null,\n onClick = {\n /* click action */\n },\n onLongClick = {\n if (isItemSelected) selectedItems -= index else selectedItems += index\n }\n )\n )\n }\n }\n }\n}https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/compose/snippets/src/main/java/com/example/compose/snippets/components/AppBar.kt#L449-L479\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- The top bar updates based on how many list items you select.\n- `selectedItems` holds the set of selected item indexes.\n- `AppBarMultiSelectionExample` uses a [`Scaffold`](/reference/kotlin/androidx/compose/material/package-summary#Scaffold(androidx.compose.foundation.layout.WindowInsets,androidx.compose.ui.Modifier,androidx.compose.material.ScaffoldState,kotlin.Function0,kotlin.Function0,kotlin.Function1,kotlin.Function0,androidx.compose.material.FabPosition,kotlin.Boolean,kotlin.Function1,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)) to structure the screen.\n - `topBar = { AppBarSelectionActions(selectedItems) }` sets the `AppBarSelectionActions` composable as the top app bar. `AppBarSelectionActions` receives the `selectedItems` state.\n- [`LazyColumn`](/reference/kotlin/androidx/compose/foundation/lazy/package-summary#LazyColumn(androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.LazyListState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.ui.Alignment.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,kotlin.Function1)) displays the items in a vertical list, rendering only the items visible on the screen.\n- `ListItemSelectable` represents a selectable list item.\n - [`combinedClickable`](/reference/kotlin/androidx/compose/foundation/package-summary#(androidx.compose.ui.Modifier).combinedClickable(androidx.compose.foundation.interaction.MutableInteractionSource,androidx.compose.foundation.Indication,kotlin.Boolean,kotlin.String,androidx.compose.ui.semantics.Role,kotlin.String,kotlin.Function0,kotlin.Function0,kotlin.Boolean,kotlin.Function0)) allows both click and long-click handling for item selection. A click performs an action, while long-clicking an item toggles its selection state.\n\nResult **Figure 2.** A list integrated into a dynamic top app bar with specific items selected.\n\nAdditional resources\n\n- [`TopAppBar`](/reference/kotlin/androidx/compose/material3/package-summary#TopAppBar(kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Function0,kotlin.Function1,androidx.compose.ui.unit.Dp,androidx.compose.foundation.layout.WindowInsets,androidx.compose.material3.TopAppBarColors,androidx.compose.material3.TopAppBarScrollBehavior))\n- [`LazyColumn`](/reference/kotlin/androidx/compose/foundation/lazy/package-summary#LazyColumn(androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.LazyListState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.ui.Alignment.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,kotlin.Function1))\n- [`Scaffold`](/reference/kotlin/androidx/compose/material/package-summary#Scaffold(androidx.compose.ui.Modifier,androidx.compose.material.ScaffoldState,kotlin.Function0,kotlin.Function0,kotlin.Function1,kotlin.Function0,androidx.compose.material.FabPosition,kotlin.Boolean,kotlin.Function1,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1))"]]