2

I'm still learning compose/kotlin and have come up with a problem that seems very common, but I can't find a solution.

I have a LazyColumn with animation--that works great for making additions and subtractions from the LazyColumn visible and attractive. But I want to have a very complicated items. They are very unwieldy, so I naturally want to have them in their own function.

But when I move the item content to a separate composable function, the inner function doesn't know how to deal with animateItemPlacement().

Here is my (simplified) code for the composable function with the LazyColumn

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyColumnAnimExample(
    stringList: List<String>,
    modifier: Modifier
) {
    LazyColumn(
        modifier = modifier
            .padding(top = 62.dp)
    ) {
        stringList.forEach { str ->
            item(key = str) {
                ComplicatedItemDraw(str)
            }
        }
    }
}

And here is the function (greatly simplified) that draws each item

@Composable
fun ComplicatedItemDraw(text: String) {
    Text(
        text = text,
        style = MaterialTheme.typography.headlineLarge,
        modifier = Modifier
            .animateItemPlacement()  // error
    )
}

The error message is "Unresolved reference: animateItemPlacement"

There has to be a way around this--probably something simple.

1 Answer 1

4

animateItemPlacement is a function defined in LazyItemScope interface, so for you to be able to use it in your function, it has to be an extension function of that interface:

@Composable
fun LazyItemScope.ComplicatedItemDraw(text: String) {
    Text(text, modifier = Modifier.animateItemPlacement())
}

This however means that you won't be able to use ComplicatedItemDraw outside of LazyList. Probably a better solution and a best practice, is to define Modifier parameter on your composable and pass animateItemPlacement() from your item {} like this:

@Composable
fun Example() {
    LazyColumn {
        item {
            ComplicatedItemDraw("", Modifier.animateItemPlacement())
        }
    }
}

@Composable
fun ComplicatedItemDraw(text: String, modifier: Modifier = Modifier) {
    Text(text, modifier = modifier)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Haha! Both work! Thank you for suggesting two ways for two different situations. Too bad I can't give you two checks.
animateItemPlacement is Deprecated, you should use animateItem or gist.github.com/lisonge/b966cbb10728e7cdc60cf3ad971b4b81

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.