Slide 1

Slide 1 text

LazyVerticalGridʹֶͿ 
 ඇεΫϩʔϥϒϧͳάϦου ͷ࡞Γํ potatotips #76 Shota Ara

Slide 2

Slide 2 text

LazyVerticalGrid • Jetpack Composeʹͯར༻Ͱ͖Δ άϦουදࣔͷComposable • σϑΥϧτͰεΫϩʔϧʹରԠ

Slide 3

Slide 3 text

LazyVerticalGridͷ࢖͍ʹ͘͞ • ൃද࣌఺ʢ2021/11/17ʣͰExperimental • LazyVerticalGridࣗମ͕εΫϩʔϥϒϧͳComposableͰ͋Δ

Slide 4

Slide 4 text

LazyVerticalGridͷ 
 ࢖͍ʹ͘͞ LazyVerticalGridࣗମ͕εΫϩʔϧ 
 ՄೳͳͷͰɺ 
 ϦετͷҰ෦͕άϦου 
 ͳUIͷදݱ͕Ͱ͖ͳ͍ɻ

Slide 5

Slide 5 text

LazyVerticalGridΛࢀߟʹComposableΛ࡞Δ • Experimentalͱ͸͍͑ެࣜͷࢀߟ࣮૷͕͋Δɻ • ComposableͳͲஔ͖׵͑Մೳͳؔ਺ʹ͓͚ͯ͠͹ 
 ΋͠StableͳAPI͕௥Ճ͞Εͨͱ͖͸ͦΕΛஔ͖׵͑Δ͚ͩͰྑ͍ɻ

Slide 6

Slide 6 text

LazyVerticalGridͷ಺෦ॲཧ

Slide 7

Slide 7 text

LazyVerticalGridͷ಺෦ॲཧ val rows = (scope.totalSize + nColumns - 1) / nColumns LazyColumn( /* modifier΍paddingͳͲ֎෦͔Β౉͖ͯͨ͠΋ͷΛόέπϦϨʔ */ ) { items(rows) { rowIndex -> Row { for (columnIndex in 0 until nColumns) { /* ֤ΞΠςϜͷComposableΛݺͼग़͠ */ } } } }

Slide 8

Slide 8 text

LazyVerticalGridͷ಺෦ॲཧ val rows = (scope.totalSize + nColumns - 1) / nColumns LazyColumn( /* modifier΍paddingͳͲ֎෦͔Β౉͖ͯͨ͠΋ͷΛόέπϦϨʔ */ ) { items(rows) { rowIndex -> Row { for (columnIndex in 0 until nColumns) { /* ֤ΞΠςϜͷComposableΛݺͼग़͠ */ } } } } ౉͞ΕͨΞΠςϜͷαΠζͱΧϥϜ਺͔Β 
 ߦ਺Λܭࢉ͢Δ

Slide 9

Slide 9 text

LazyVerticalGridͷ಺෦ॲཧ val rows = (scope.totalSize + nColumns - 1) / nColumns LazyColumn( /* modifier΍paddingͳͲ֎෦͔Β౉͖ͯͨ͠΋ͷΛόέπϦϨʔ */ ) { items(rows) { rowIndex -> Row { for (columnIndex in 0 until nColumns) { /* ֤ΞΠςϜͷComposableΛݺͼग़͠ */ } } } } ಺෦ͰLazyColumnΛ͍࣋ͬͯΔ 
 ʹ εΫϩʔϧͰ͖Δਖ਼ମ

Slide 10

Slide 10 text

LazyVerticalGridͷ಺෦ॲཧ val rows = (scope.totalSize + nColumns - 1) / nColumns LazyColumn( /* modifier΍paddingͳͲ֎෦͔Β౉͖ͯͨ͠΋ͷΛόέπϦϨʔ */ ) { items(rows) { rowIndex -> Row { for (columnIndex in 0 until nColumns) { /* ֤ΞΠςϜͷComposableΛݺͼग़͠ */ } } } } ҰߦҰߦΛRowʹͯ͠itemsͰ 
 ͦΕΛॎʹੵΜͰߦ͘ॲཧ

Slide 11

Slide 11 text

͜ΕΛࢀߟʹ…

Slide 12

Slide 12 text

LazyVerticalGridΛࢀߟʹͨ͠ඇεΫϩʔϧ൛ val rows = (items.size + columns - 1) / columns Column( /* modifier΍paddingͳͲ֎෦͔Β౉͖ͯͨ͠΋ͷΛόέπϦϨʔ */ ) { for (rowIndex in 0..rows) { Row { for (columnIndex in 0 until nColumns) { /* ֤ΞΠςϜͷComposableΛݺͼग़͠ */ } } } }

Slide 13

Slide 13 text

໰୊఺

Slide 14

Slide 14 text

αϯϓϧ࣮૷ LazyColumn { item { NonScrollableGrid( column = 3, items = myItems ) { /* ֤ΞΠςϜͷComposable */ } } }

Slide 15

Slide 15 text

໰୊఺ LazyColumn { item { NonScrollableGrid( column = 3, items = (0..1000).toList() ) { /* ֤ΞΠςϜͷComposable */ Log.d(“@@@”, “$it”) } } }

Slide 16

Slide 16 text

໰୊఺ LazyColumn { item { NonScrollableGrid( column = 3, items = (0..1000).toList() ) { /* ֤ΞΠςϜͷComposable */ Log.d(“@@@”, “$it”) } } } 0…1000😇

Slide 17

Slide 17 text

໰୊఺ LazyColumn { item { NonScrollableGrid( column = 3, items = (0..1000).toList() ) { /* ֤ΞΠςϜͷComposable */ Log.d(“@@@”, “$it”) } } } item͕composableؔ਺ 
 ݺͼग़͠ͷ୯Ґ

Slide 18

Slide 18 text

վળ

Slide 19

Slide 19 text

fun LazyListScope.gridItems( columns: Int, items: List, gridItem: @Composable (T) -> Unit ) { val rows = (items.size + columns - 1) / columns items(rows) { rowIndex -> Row { for (columnIndex in 0 until nColumns) { val itemIndex = rowIndex * columns + columnIndex if (itemIndex < items.size) { Box( modifier = Modifier.weight(1f, fill = true), propagateMinConstraints = true ) { gridItem.invoke(items[itemIndex]) } } else { Spacer(Modifier.weight(1f, fill = true)) } } } } }

Slide 20

Slide 20 text

໰୊఺ LazyColumn { gridItems( column = 3, items = (0..1000).toList() ) { /* ֤ΞΠςϜͷComposable */ Log.d(“@@@”, “$it”) } }

Slide 21

Slide 21 text

໰୊఺ LazyColumn { gridItems( column = 3, items = (0..1000).toList() ) { /* ֤ΞΠςϜͷComposable */ Log.d(“@@@”, “$it”) } } 0…24

Slide 22

Slide 22 text

·ͱΊ

Slide 23

Slide 23 text

·ͱΊ • Jetpack Composeʹ͸άϦου഑ஔͷComposable͕༻ҙͯ͋͠Δɻ • ͨͩ͠࢖͍ʹ͍͘໘΋ɻ • ಺෦ίʔυ͸ൺֱతγϯϓϧͳͷͰ͜ΕΛࢀߟʹܨ͗ͷ࣮૷͕Մೳɻ • LazyColumnͷඳըͷ୯Ґ͸֤itemͰ͋Δɻ