无法使用新的Modifier.animateItem()

问题描述 投票:0回答:1

我想使用新的

Modifier.animateItem()
API(在 herehere 中宣布)来为
LazyColumn
内的项目(dis)外观设置动画,但是我不可能导入它。我已将
"androidx.compose.animation:animation:1.7.0-alpha06"
添加到我的依赖项中,并尝试在
LazyListScope
中使用它,但 AndroidStudio 找不到它。我是不是错过了什么?

android kotlin android-jetpack-compose
1个回答
0
投票

您不需要导入它,因为它不是顶级函数。它是

LazyItemScope
的成员函数,因此每当你有这样一个对象时,你只需调用
animateItem
即可。实际上,这看起来有点不同,因为您需要一个额外的
Modifier
接收器对象。

通常它看起来像这样:

LazyColumn {
    items(listOfItems) {
        Box( // or whatever you use to display an item
            modifier = Modifier.animateItem(),
        )
    }
}

items
lambda 提供了
LazyItemScope
,因此现在您可以使用
Modifier
来调用
animateItem
,它将使用这两个接收器对象。

请注意,

animateItem
不适用于
LazyColumn
。它在
LazyColumn
的 lambda(提供
LazyListScope
)中也不可用。它仅适用于提供
item
的各种
items
LazyItemScope
lambda。

© www.soinside.com 2019 - 2024. All rights reserved.