1.导入库
implementation("androidx.lifecycle:lifecycle-viewmodel:2.8.7")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7")
implementation("androidx.navigation:navigation-common:2.8.9")
implementation("androidx.navigation:navigation-compose:2.8.9")
implementation("androidx.compose.material3:material3:1.3.2")
implementation("androidx.compose.material3.adaptive:adaptive:1.1.0")
implementation("androidx.compose.material3.adaptive:adaptive-layout-android:1.1.0")
implementation("androidx.compose.material3.adaptive:adaptive-navigation-android:1.1.0")
implementation("androidx.compose.material3.adaptive:adaptive-navigation:1.1.0")
implementation("androidx.compose.material3:material3-adaptive-navigation-suite:1.3.2")
implementation("androidx.compose.material:material:1.8.0")
implementation("androidx.compose.material:material-icons-extended:1.7.8")
implementation("androidx.compose.material:material-icons-core:1.7.8")
implementation("androidx.compose.material3:material3-window-size-class:1.3.2")
2.示例代码
//创建ViewModel类,可以单独一个文件,也可以放在使用的类中。
class AnimatedOrderedListViewModel : ViewModel() {
private val _data =
listOf("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten")
private val _displayedItems: MutableStateFlow<List<String>> = MutableStateFlow(_data)
val displayedItems: StateFlow<List<String>> = _displayedItems
fun resetOrder() {
_displayedItems.value = _data.filter { it in _displayedItems.value }
}
fun sortAlphabetically() {
_displayedItems.value = _displayedItems.value.sortedBy { it }
}
fun sortByLength() {
_displayedItems.value = _displayedItems.value.sortedBy { it.length }
}
fun addItem() {
// Avoid duplicate items
val remainingItems = _data.filter { it !in _displayedItems.value }
if (remainingItems.isNotEmpty()) _displayedItems.value += remainingItems.first()
}
fun removeItem() {
_displayedItems.value = _displayedItems.value.dropLast(1)
}
}
//创建界面
@Composable
private fun ListAnimatedItemsExample(
data: List<String>,
modifier: Modifier = Modifier,
onAddItem: () -> Unit = {},
onRemoveItem: () -> Unit = {},
resetOrder: () -> Unit = {},
onSortAlphabetically: () -> Unit = {},
onSortByLength: () -> Unit = {},
) {
val canAddItem = data.size < 10
val canRemoveItem = data.isNotEmpty()
Scaffold(modifier) { paddingValues ->
Column(
modifier = Modifier
.padding(paddingValues)
.fillMaxSize()
) {
// Buttons that change the value of displayedItems.
AddRemoveButtons(canAddItem, canRemoveItem, onAddItem, onRemoveItem)
OrderButtons(resetOrder, onSortAlphabetically, onSortByLength)
// List that displays the values of displayedItems.
ListAnimatedItems(data)
}
}
}
//其中使用的函数
@Composable
private fun AddRemoveButtons(
canAddItem: Boolean,
canRemoveItem: Boolean,
onAddItem: () -> Unit,
onRemoveItem: () -> Unit
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
Button(enabled = canAddItem, onClick = onAddItem) {
Text("Add Item")
}
Spacer(modifier = Modifier.padding(25.dp))
Button(enabled = canRemoveItem, onClick = onRemoveItem) {
Text("Delete Item")
}
}
}
// [END android_compose_layouts_list_addremovebuttons]
// [START android_compose_layouts_list_orderbuttons]
@Composable
private fun OrderButtons(
resetOrder: () -> Unit,
orderAlphabetically: () -> Unit,
orderByLength: () -> Unit
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
var selectedIndex by remember { mutableIntStateOf(0) }
val options = listOf("Reset", "Alphabetical", "Length")
SingleChoiceSegmentedButtonRow {
options.forEachIndexed { index, label ->
SegmentedButton(
shape = SegmentedButtonDefaults.itemShape(
index = index,
count = options.size
),
onClick = {
Log.d("AnimatedOrderedList", "selectedIndex: $selectedIndex")
selectedIndex = index
when (options[selectedIndex]) {
"Reset" -> resetOrder()
"Alphabetical" -> orderAlphabetically()
"Length" -> orderByLength()
}
},
selected = index == selectedIndex
) {
Text(label)
}
}
}
}
}
@Composable
fun ListAnimatedItems(
items: List<String>,
modifier: Modifier = Modifier
) {
LazyColumn(modifier) {
// Use a unique key per item, so that animations work as expected.
items(items, key = { it }) {
ListItem(
headlineContent = { Text(it) },
modifier = Modifier
.animateItem(
// Optionally add custom animation specs
)
.fillParentMaxWidth()
.padding(horizontal = 8.dp, vertical = 0.dp),
)
}
}
}
//调用ListAnimatedItemsExample()
@Composable
fun AnimatedOrderedListScreen(
viewModel: AnimatedOrderedListViewModel,
modifier: Modifier = Modifier,
) {
val displayedItems by viewModel.displayedItems.collectAsStateWithLifecycle()
ListAnimatedItemsExample(
displayedItems,
onAddItem = viewModel::addItem,
onRemoveItem = viewModel::removeItem,
resetOrder = viewModel::resetOrder,
onSortAlphabetically = viewModel::sortAlphabetically,
onSortByLength = viewModel::sortByLength,
modifier = modifier
)
}
3.效果