Closed sultansse closed 9 months ago
There are some ways to check what is cause of lag and how to optimize it.
Layout Inspector Measure how many recomposition is occurred. Layout Inspector is Android Studio's feature.
Compose Compiler Metrics You can check what class is stable or unstable, what composable function is skippable or unskippable. Useful link: https://chrisbanes.me/posts/composable-metrics/
Migrate List
to ImmutableList
List
interface is considered unstable by compose compiler. You can use ImmutableList
of Kotlin Immutable Collection library. Compose Compiler considers ImmutableList
as stable.
Checkout https://github.com/Kotlin/kotlinx.collections.immutable
Optimize LazyColumn
with key
You can optimize LazyColumn
with key
that is a parameter of LazyListScope.items()
. key
makes lazy list's item to unique. If item is unique, the lazy list performs more efficiency when recomposing items.
i tried to make it immutableList, and checked recompositions (used rebugger and layout inspector, but it did not helped, it seems like there is no recompose effect)
here is my code, interesting part is that when i added a lot of note items, and scrolll screen where only notes - it works pretty ok, but top place (where placed only storyly and gridlayout and notes) is very laggy( i even tried to sign app and enabled minifying true debuggle = false and made relase verision, it did not helped
why it can be because of Stories? - becasue when i deleted that part from ui, and leaved only notes and grid - it was still laggy, when only notes - no lag
i am sorry for being newbie, and thank you for your time and attention!
How about to add key
to item? For example:
LazyColumn {
item(
key = { /* something uniqe key */ }
) {
Caterogories()
}
}
And if the category list will not changed, try remember it.
val categories = remember { /* init list*/ }
After them, try build app as release build varient.
i tried this key
, but it did not showed me big result, so then i got - each categoryItem
had and resource drawable, and i added COIL
to load them optimized (rememberAsyncImagePainter
) - and it seems like its helped!
so, thank you! For this amazing library, cause i really had troubles with same weighted grid layout, and thank you for your responses and effort!
Ah, so image loading was the cause of low performance!
Thanks for using my library.
Issue close because question is solved.
` data class CategoryDto( val icon: Int, val title: String )
@Composable fun Categories() { val categories = listOf( CategoryDto( icon = R.drawable.img_ai, title = "Ai assistant" ), CategoryDto( icon = R.drawable.img_gmail, title = "Gmail" ), CategoryDto( icon = R.drawable.img_mysdu, title = "MySDU" ), CategoryDto( icon = R.drawable.img_sdukz, title = "Sdu.kz" ), CategoryDto( icon = R.drawable.img_mysdu, title = "Student clubs" ), CategoryDto( icon = R.drawable.img_free_offices, title = "Free Offices" ), CategoryDto( icon = R.drawable.img_moodle, title = "Moodle" ), CategoryDto( icon = R.drawable.img_library, title = "Library" ), )
}
@Composable fun Category( icon: Int, title: String, onCategoryClick: () -> Unit, ) { Column( horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, modifier = Modifier .padding(horizontal = 2.dp, vertical = 8.dp) .clickable { onCategoryClick() } ) { Image( painter = painterResource(icon), contentDescription = title, modifier = Modifier .size(56.dp) .shadow(elevation = 4.dp, shape = CircleShape) .clip(CircleShape) ) Text( text = title, textAlign = TextAlign.Center, ) } } ` At first - i want to say thank you for easying our job with this awsome library! buuut here is my code, and i dont know why its laggy. in homescreen i just show in lazyColumn as item this gridlayout and as next item column of notes (just simple notes with room). without this grid - notes work well, i need optimizations, please help