CS 1998- Intro to Android Development
LeC 2: Modifiers, Lazylists and
The Reactive UI paradigm
2
Kahoot!
Announcements
3
Modifiers
Modifiers
5
Example
Usage
6
Some common Modifier Functions
7
Order is important!
8
Lazylists
What is a lazylist
10
11
Instead of creating new cells, we’re re-rendering existing cells and binding new data to them.
Lazyrow
LazyColumn
Lazygrid
12
Example
Usage
13
Usage
with a given dataset
14
Reactive ui
Declarative programming
Imperative programming
Describes what the program should accomplish, rather than how to achieve it.
Describes the steps necessary to achieve a particular result.
Describes how to achieve the result
16
17
Also used by Flutter and SwiftUI
18
Reactive UI
Falls under declarative programming
The UI reacts to the changes in data without you explicitly asking it to with each change.
19
20
State
Represents data that can change over time
21
Using states
Instead of saying
var text = “Hello”
We say
var text by remember {mutableStateOf(“Hello”)}
Here, “text” is a state variable
22
23
whenever “text” changes, we have to explicitly recompose the UI components that use “text” to show change in the UI
var greeting = "Hello"
Text(text = greeting)
greeting = "Goodbye"
//the value in Text() //won’t change
Why use states instead?
The previous method can be problematic, as whenever a variable changes you have to call all the UI components that depend on it to change them.
24
25
However, if we use the second method, any UI component that reads from the text automatically recomposes to show any change in the value “text”
var greeting by remember {mutableStateOf("Hello")}
Text(greeting)
greeting = "Goodbye"
you create the components and pipeline the data in
26
Let’s break this down
27
var name by remember { mutableStateOf(“”) }
By – Allows “Name” to be used as a regular variable instead of a state
Remember – Used to remember the value of a state across recompositions. When the composable function recomposes, remember ensures that the state value is preserved and not reinitialized.
mutableStateOf() – initializes what’s inside the variable as a mutable value
28
var name by remember { mutableStateOf(“”) }
Example
29
</Lecture>
30
Demo time!
Text composable and button below it, both centered on the screen.
The initial value of the text should be “Hello”
When the button is pressed, the value should change to “Goodbye”
31