๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ| Android/๐Ÿš€ | Jetpack

[Android, Kotlin] Compose UI ์ •๋ฆฌ(4)

by immgga 2022. 12. 5.

์ง€๋‚œ ํฌ์ŠคํŒ…

https://rkdrkd-history.tistory.com/43

 

[Android, Kotlin] Compose UI ์ •๋ฆฌ(3)

์ด์ „ ํฌ์ŠคํŒ… https://rkdrkd-history.tistory.com/42 [Android, Kotlin] Compose UI ์ •๋ฆฌ(2) ์ง€๋‚œ ํฌ์ŠคํŒ… https://rkdrkd-history.tistory.com/41 [Android, Kotlin] Compose UI ์ •๋ฆฌ(1) 1. layout 1. Column ํ•˜์œ„์˜ ๋ทฐ๋“ค์„ ์ˆ˜์ง ์ •๋ ฌํ•ด์ค€๋‹ค(verti

rkdrkd-history.tistory.com

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค˜๋˜ compose recyclerview๋ฅผ ์ด์šฉํ•ด ์‹ค์Šต์„ ํ•ด๋ณด๊ฒ ๋‹ค.

 

์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค ์ƒ์„ฑ

data class Practice2Data(
    val name: String,
    val age: Int,
    val phoneNum: String
)

๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ฆ„, ๋‚˜์ด, ์ „ํ™”๋ฒˆํ˜ธ๋งŒ ์ค˜๋ดค๋‹ค.

 

recyclerView ์•„์ดํ…œ ์ƒ์„ฑ

@Composable
fun ExampleItem(item: Practice2Data) {
    Card(
        modifier = Modifier
            .padding(10.dp)
            .fillMaxWidth()
            .height(100.dp)
    ) {
        // ๋ฐฐ๊ฒฝ ๋ฐ•์Šค
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.LightGray)
                .padding(start = 15.dp, end = 15.dp)
        ) {
        }

        Box(
            contentAlignment = Alignment.CenterStart
        ) {
            Column(modifier = Modifier.offset(x = 10.dp)) {
                Text(text = "์ด๋ฆ„:"+item.name, modifier = Modifier.offset(y = (-5).dp))
                Text(text = "๋‚˜์ด:"+item.age.toString(), modifier = Modifier.offset(y = 5.dp))
            }
        }

        Box(
            contentAlignment = Alignment.CenterEnd,
            modifier = Modifier.offset(x = (-5).dp)
        ) {
            Text(text = "phone:"+item.phoneNum)
        }
    }
}

 

recyclerview ์ƒ์„ฑ

@Preview(showBackground = true)
@Composable
fun DefaultPreview3() {
    val item = mutableListOf<Practice2Data>()
    item.add(0, Practice2Data("name1", 16, "010-1234-5678"))
    item.add(1, Practice2Data("name2", 18, "010-2345-6789"))
    item.add(2, Practice2Data("name3", 20, "010-3456-7890"))
    item.add(3, Practice2Data("name4", 18, "010-1111-2222"))
    item.add(4, Practice2Data("name5", 20, "010-2222-3333"))
    item.add(5, Practice2Data("name6", 24, "010-3333-4444"))
    item.add(6, Practice2Data("name7", 15, "010-4444-5555"))

    ComposeUITheme {
        LazyColumn {
            itemsIndexed(item) { index, data ->  
                ExampleItem(item = data)
            }
        }
    }
}

 

์‹คํ–‰ ๊ฒฐ๊ณผ

์ •์ƒ์ ์œผ๋กœ ์ž˜ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

728x90