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
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
DefaultTheme { | |
// A surface container using the 'background' color from the theme | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colorScheme.background | |
) { | |
LazyVerticalGridExampleWithNavigation() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun LazyVerticalGridExampleWithNavigation() { | |
val numbers = (1..100).toList() | |
val navController = rememberNavController() | |
NavHost(navController = navController, startDestination = "gridScreen") { | |
composable("gridScreen") { | |
LazyVerticalGrid( | |
columns = GridCells.Fixed(2), | |
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp) | |
) { | |
items(numbers) { number -> | |
GridItem(number) { | |
navController.navigate("detailScreen/$number") | |
} | |
} | |
} | |
} | |
composable("detailScreen/{number}") { backStackEntry -> | |
val number = backStackEntry.arguments?.getString("number") | |
if (number != null) { | |
DetailScreen(number = number.toInt()) | |
} | |
} | |
} | |
} | |
@Composable | |
fun GridItem(number: Int, onItemClick: () -> Unit) { | |
Box( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(8.dp) | |
.height(100.dp) | |
.background(Color.LightGray) | |
.clickable { onItemClick.invoke() }, | |
contentAlignment = Alignment.Center | |
) { | |
Text( | |
text = number.toString(), | |
fontSize = 24.sp, | |
fontWeight = FontWeight.Bold, | |
color = Color.Black | |
) | |
} | |
} | |
@Composable | |
fun DetailScreen(number: Int) { | |
Box( | |
modifier = Modifier.fillMaxSize(), | |
contentAlignment = Alignment.Center | |
) { | |
Text( | |
text = "Selected Number: $number", | |
fontSize = 24.sp, | |
fontWeight = FontWeight.Bold, | |
color = Color.Black | |
) | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun GreetingPreview() { | |
DefaultTheme { | |
LazyVerticalGridExampleWithNavigation() | |
} | |
} |

'Compose' 카테고리의 다른 글
사진첩 앱 리스트 (0) | 2023.05.13 |
---|---|
Box (0) | 2023.05.13 |
LazyVerticalGrid (0) | 2023.05.11 |
compose 계산기 수정 (0) | 2023.05.10 |
compose 계산기 (0) | 2023.05.10 |