Movies UI - Lists

Convert to LazyColumn

Our first step to creating a dynamic UI is to change our normal Column to a LazyColumn.

Note

LazyColumn comes with its own scrolling. To make it work properly, you must remove the verticalScroll modifier when switching from Column to LazyColumn.

The content lambda for LazyColumn contains an items call. Here we specify the list of items to use, and how to map an id to each. We know MovieDto has an id, so we can just use it as the id for the displayed row. Using a key allows LazyColumn to better optimize its recomposition of its rows, and later we'll see that it also supports animation of added or deleted rows.

Code Changes

CHANGED: /app/src/main/java/com/androidbyexample/movie/screens/MovieListUi.kt
package com.androidbyexample.movie.screens

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsimport androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.androidbyexample.movie.R
import com.androidbyexample.movie.components.Display
import com.androidbyexample.movie.repository.MovieDto

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MovieListUi(
    movies: List<MovieDto>,
    onMovieClicked: (MovieDto) -> Unit,
    onResetDatabase: () -> Unit,
) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(text = stringResource(R.string.movies)) },
                actions = {
                    IconButton(onClick = onResetDatabase) {
                        Icon(
                            imageVector = Icons.Default.Refresh,
                            contentDescription = stringResource(R.string.reset_database)
                        )
                    }
                }
            )
        },
        modifier = Modifier.fillMaxSize()
    ) { paddingValues ->
// Column( LazyColumn( modifier = Modifier .padding(paddingValues) .fillMaxSize() // .verticalScroll(rememberScrollState()) ) { // movies.forEach { movie -> items( items = movies, key = { it.id }, ) { movie -> Card( elevation = CardDefaults.cardElevation( defaultElevation = 8.dp, ), onClick = { onMovieClicked(movie) }, modifier = Modifier.padding(8.dp) ) { Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(8.dp) ) { Icon( imageVector = Icons.Default.Star, contentDescription = stringResource(id = R.string.movie) ) Display(text = movie.title) } } } }
} }