-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from myofficework000/plus_lesson_3_toggle_list
Plus lesson 3 Toggle List content
- Loading branch information
Showing
1 changed file
with
58 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 58 additions & 1 deletion
59
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_3/Toggle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,65 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_3 | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.grid.GridCells | ||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
import androidx.compose.foundation.lazy.grid.items | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun ToggleList(){ | ||
fun ToggleList() { | ||
var checked by remember { mutableStateOf(false) } | ||
val lists: List<String> = listOf("A", "B", "C", "D") | ||
Column { | ||
Switch( | ||
checked = checked, | ||
onCheckedChange = { | ||
checked = it | ||
} | ||
) | ||
if (checked) { | ||
GridView(lists) | ||
} else { | ||
ListView(lists) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun GridView(lists: List<String>) { | ||
LazyVerticalGrid(columns = GridCells.Adaptive(minSize = 128.dp), | ||
content = { | ||
items(lists) { | ||
Text( | ||
text = it, | ||
modifier = Modifier.padding(40.dp) | ||
) | ||
} | ||
} | ||
) | ||
} | ||
|
||
@Composable | ||
fun ListView(lists: List<String>) { | ||
LazyColumn( | ||
content = { | ||
items(lists) { | ||
Text( | ||
text = it, | ||
modifier = Modifier.padding(8.dp) | ||
) | ||
} | ||
} | ||
) | ||
} |