# Expandable lists in Jetpack Compose

DevFeed: [Expandable lists in Jetpack Compose](<https://devfeed.tech/articles/expandable-lists-in-jetpack-compose-25896.md>)

Original publisher: [Read original article](<https://proandroiddev.com/expandable-lists-in-jetpack-compose-b0b78c767b4?source=rss-56174fa84bcc------2>)

Author: Denys Rudenko

Published: 2021-01-11T18:21:04Z

Content type: tutorial

Language: en

Sources: [Stories by Denis Rudenko on Medium](<https://devfeed.tech/sources/stories-by-denis-rudenko-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [ui](<https://devfeed.tech/topics/ui.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [compose](<https://devfeed.tech/tags/compose.md>), [constraintlayout](<https://devfeed.tech/tags/constraintlayout.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [expandable-animation](<https://devfeed.tech/tags/expandable-animation.md>), [expandable-list](<https://devfeed.tech/tags/expandable-list.md>), [expandable-view](<https://devfeed.tech/tags/expandable-view.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [layout](<https://devfeed.tech/tags/layout.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [memory](<https://devfeed.tech/tags/memory.md>), [state](<https://devfeed.tech/tags/state.md>), [stateflow](<https://devfeed.tech/tags/stateflow.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

A tutorial explains how to build expandable lists in Jetpack Compose. It uses a card data class, a view model with StateFlow for cards and expanded card IDs, composable functions that observe state, and transition state to animate card expansion.

## Source excerpt

Expandable views are a common way to hide details of a visualised data structures. Let's take a look at how the following can be achieved in 6 steps, using compose: https://medium.com/media/b9dface7145a04eb3bee8ac03eed3a4e/hrefStep 1: Creating a data class for carddata class ExpandableCardModel(val id: Int, val title: String)Step 2: Create a data source We'll be using AAC viewModel example here, but feel free to use any "controller" abstraction you like. https://medium.com/media/61d179f82414c569e4d8e415bbed8aa6/href This class serves 4 purposes: Holds list of cards using MutableStateFlow in _cards field, and exposes a StateFlow to observers via cards field. Holds list of expanded card ids in _expandedCardIdsList, and exposes them to observers via expandedCardIdsList field. Provides list of cards using getFakeData() function. We need a coroutine here, to emit the testList into _cards. Contains a onCardArrowClicked() to mark cards as expanded by adding tapped card id to _expandedCardIdsList, and notify observers about this change by mutating the state of _expandedCardIdsList. Step 3: MainActivityhttps://medium.com/media/6e10348f08fc4004c3a397cd46c19bd0/href We are initialising the CardsViewModel here, and providing it to the CardsScreen composable function. Step 4: CardsScreen composablehttps://medium.com/media/b2cd50c3ab379cd75897eda6b1aca4c6/href We are observing the viewModel.cards containing our list of cards, and viewModel.expandedCardIds with the help of .collectAsState(). What's the purpose of .collectAsState()? It's converting StateFlow into a State. Update: using .collectAsStateWithLifecycle() which is the same thing, but lifecycle aware, and helps to reduce memory consumption when UI is not visible to the user. More info in this article. What's a State? From the documentation: " State is a value holder where reads to the value property during the execution of a composable function, the current recomposeScope will be subscribed to changes of that value." Step