Advertisement
Upar

grid, var position value.

May 20th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. private val items = listOf(
  2. Icons.Filled.Check,
  3. Icons.Filled.Close,
  4. Icons.Filled.ThumbUp,
  5. Icons.Filled.Build,
  6. Icons.Filled.Delete,
  7. Icons.Filled.Home,
  8. Icons.Filled.Close,
  9. Icons.Filled.ThumbUp,
  10. Icons.Filled.Build,
  11. Icons.Filled.ThumbUp,
  12. )
  13.  
  14. @Composable
  15. fun GridScreen() {
  16. GridView(columnCount = 3)
  17.  
  18. BackButtonHandler {
  19. JetFundamentalsRouter.navigateTo(Screen.Navigation)
  20. }
  21. }
  22.  
  23. @Composable
  24. fun GridView(columnCount: Int) {
  25. val itemSize= items.size
  26. val rowCount= ceil(itemSize.toFloat() / columnCount).toInt()
  27. val gridItems= mutableListOf<List<IconResource>>()
  28. var position= 0
  29.  
  30. for(i in 0 until rowCount){
  31. val rowItem= mutableListOf<IconResource>()
  32. for(j in 0 until columnCount){
  33.  
  34.  
  35. //idk the position value, init 0, when it get changed?
  36. //dialog appear inc() in android studio just tell "Increment this value"
  37. //idk when this condition true or false because idk the value...0? the inc() increment like how many?
  38. if(position.inc() <= itemSize){
  39. rowItem.add(IconResource(items[position++],true))
  40. }
  41. }
  42. val itemToFill= columnCount - rowItem.size
  43. for(j in 0 until itemToFill){
  44. rowItem.add(IconResource(Icons.Filled.Delete, false))
  45. }
  46. gridItems.add(rowItem)
  47. }
  48. LazyColumn(modifier= Modifier.fillMaxSize()) {
  49. items(gridItems){items ->
  50. RowItem(items)
  51. }
  52. }
  53. }
  54.  
  55. @Composable
  56. fun RowItem(rowItems: List<IconResource>) {
  57. Row{
  58. for(element in rowItems)
  59. GridIcon(element)
  60. }
  61. }
  62.  
  63. @Composable
  64. fun RowScope.GridIcon(iconResource: IconResource) {
  65. val color= if(iconResource.isVisible)
  66. colorResource(R.color.colorPrimary)
  67. else Color.Transparent
  68. Icon(
  69. imageVector =iconResource.imageVector,
  70. tint = color,
  71. contentDescription = stringResource(R.string.grid_icon),
  72. modifier = Modifier
  73. .size(80.dp,80.dp)
  74. .weight(1f)
  75. )
  76. }
  77.  
  78. data class IconResource(val imageVector: ImageVector, val isVisible: Boolean)
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement