์๋๋ก์ด๋๋ ์๋ ๋ถํฐ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ดํฐ๋ฆฌ์คํธ๋ฅผ View๋ก ๋ํ๋ ๋
์ฌ๋ฌ ๊ฐ์ง ์ฒ๋ฆฌํด์ผ ํ ๋ถ๋ถ์ด ๋ง์๋ค. ์ด๋ฌํ ๋ถ๋ถ์ ํํผํ๊ณ ์ Android ์์ Layout ๋ถ๋ถ์ธ XML ๋ถ๋ถ๊ณผ Activity ๋ถ๋ถ์ ํฉ์ณ์ Jetpack Compose ๋ผ๋ ๊ธฐ์ ์ ๋ง๋ค์ด ๋๋ค.
LazyColumn ์ item ๊ธฐ๋ฐ์ผ๋ก ์๋ํฉ๋๋ค. item ํ๋๊ฐ ์์ ์ปดํฌ์ ๋ธ์ ๋์๋๋ฉฐ LazyColumnScope๋ด์์ ๋ค์์ item์ ์ฌ์ฉํ ์ ์์ต๋๋ค
LazyColumn ๋ด๋ถ์ ์ปดํฌ์ ๋ธ์ ์ง์ ๋ฃ๊ณ ์ถ์๋ ์ฌ์ฉํ๋ ๋ฉ์๋ ์ ๋๋ค. (LazyItemScope์ ์๋ ์ปดํฌ์ ๋ธ์ด ํ๋์ Item์ผ๋ก ์ธ์๋๋ค)
fun item(key: Any? = null, content: @Composable LazyItemScope.() -> Unit)
์ปดํฌ์ ๋ธ์ ๋ฐ๋ณตํด์ ๋ํ๋ด๊ณ ์ ํ ๋ ์ฌ์ฉํฉ๋๋ค
fun items(
count: Int,
key: ((index: Int) -> Any)? = null,
itemContent: @Composable LazyItemScope.(index: Int) -> Unit
)
- ์ค์ ์ ํ์ ๊ฐ๋ฐํ ๋ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ ๋ฐฉ์์ ๋๋ค
- ์์ดํ ์ผ๋ก Customํ ํด๋์ค์ ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
inline fun <T> LazyListScope.itemsIndexed(
items: List<T>,
noinline key: ((index: Int, item: T) -> Any)? = null,
crossinline itemContent: @Composable LazyItemScope.(index: Int, item: T) -> Unit
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MessageList()
}
}
}
@Composable
fun MessageList(){
LazyColumn{
item{
Text("First item")
}
items(5){ index ->
Text(text = "item :$index")
}
item{
Text(text="Last Item")
}
}
}
data class Person (
val id : Int,
val firstName : String,
val lastName :String,
val age :Int
)
class PersonRepository {
fun getAllData(): List<Person> {
return listOf(
Person(
id = 0,
firstName = "John",
lastName = "Doe",
age = 20
),
Person(
id = 1,
firstName = "Maria",
lastName = "Garcia",
age = 21
),
Person(
id = 2,
firstName = "James",
lastName = "Johnson",
age = 22
),
Person(
id = 3,
firstName = "Michael",
lastName = "Brown",
age = 23
),
Person(
id = 4,
firstName = "Robert",
lastName = "Davis",
age = 24
),
Person(
id = 5,
firstName = "Jenifer",
lastName = "Miller",
age = 25
),
Person(
id = 6,
firstName = "Sarah",
lastName = "Lopez",
age = 26
),
Person(
id = 7,
firstName = "Charles",
lastName = "Wilson",
age = 27
),
Person(
id = 8,
firstName = "Daniel",
lastName = "Taylor",
age = 28
),
Person(
id = 9,
firstName = "Mark",
lastName = "Lee",
age = 29
),
)
}
}
@Composable
fun CustomItem(person: Person) {
Row(
modifier = Modifier
.background(Color.LightGray)
.fillMaxWidth()
.padding(24.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
Text(
text = "${person.age}",
color = Color.Black,
fontSize = 50.sp,
fontWeight = FontWeight.Bold
)
Text(
text = person.firstName,
color = Color.Black,
fontSize = 30.sp,
fontWeight = FontWeight.Normal
)
Text(
text = person.lastName,
color = Color.Black,
fontSize = 30.sp,
fontWeight = FontWeight.Normal
)
}
}
@Composable
@Preview
fun CustomItemPreview() {
CustomItem(
person = Person(
id = 0,
firstName = "John",
lastName = "Doe",
age = 20
)
)
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val personRepository =PersonRepository()
val getAllData = personRepository.getAllData()
LazyColumn(contentPadding = PaddingValues(all = 12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)){
items(items = getAllData){ person ->
CustomItem(person = person)
}
}
}
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val personRepository = PersonRepository()
val getAllData = personRepository.getAllData()
LazyColumn(
contentPadding = PaddingValues(all = 12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
itemsIndexed(
items = getAllData,
key = { index ,person ->
person.id
}
) { index, person ->
Log.d("MainActivity", index.toString())
CustomItem(person = person)
}
}
}
}
}
class MainActivity : ComponentActivity() {
@ExperimentalFoundationApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val sections = listOf("A", "B", "C", "D", "E", "F", "G")
LazyColumn(
contentPadding = PaddingValues(all = 12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
sections.forEach { section ->
stickyHeader {
Text(
modifier = Modifier
.fillMaxSize()
.background(Color.Gray)
.padding(12.dp),
text = "Section $section"
)
}
items(10){
Text(text = "Item $it from the section $section",
modifier = Modifier.padding(12.dp))
}
}
}
}
}
}
๋ค์์ LazyRow
์ฐธ๊ณ ์๋ฃ