Anonymous
LazyColumn отстает от Jetpack Compose
Сообщение
Anonymous » 26 май 2024, 00:20
Я создал простое 30-дневное приложение, используя lazyColumn, но когда я запускаю его на своем телефоне, оно имеет ОГРОМНЫЕ задержки.
Код: Выделить всё
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
_30DaysTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
_30DaysApp()
}
}
}
}
}
@Composable
fun _30DaysApp() {
Scaffold(topBar = {
TopBar()
}) {
LazyColumn(
contentPadding = it,
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
itemsIndexed(items = tasks,
key = { _, task ->
task.id
}) { index, task ->
CardTask(task = task, day = index + 1)
}
}
}
}
@Composable
fun CardTask(task: Task, day: Int, modifier: Modifier = Modifier) {
var expand by remember { mutableStateOf(false )}
Card(shape = RoundedCornerShape(16.dp),
modifier = Modifier
.padding(horizontal = 8.dp)
.clickable { expand = !expand }) {
Column(
modifier = modifier
.animateContentSize(
animationSpec = spring(
dampingRatio = Spring.DampingRatioNoBouncy,
stiffness = Spring.StiffnessMedium
)
)
.padding(12.dp)
) {
Row {
Text(
text = stringResource(R.string.day, day),
fontSize = 16.sp,
fontWeight = FontWeight.Bold
)
Spacer(modifier = modifier.weight(1f))
ExpandButton(
expanded = expand,
modifier = Modifier.size(24.dp)
)
}
Text(
text = stringResource(task.articleRes),
fontSize = 24.sp
)
Spacer(modifier = Modifier.size(8.dp))
Image(
painter = painterResource(task.imageRes), contentDescription = null,
contentScale = ContentScale.FillWidth,
modifier = Modifier
.fillMaxWidth()
.height(160.dp)
.clip(RoundedCornerShape(12.dp)),
)
if (expand) {
Text(
text = stringResource(task.descriptionRes),
fontFamily = Poppins,
modifier = Modifier.padding(top = 12.dp)
)
}
}
}
}
@Composable
fun ExpandButton(expanded: Boolean, modifier: Modifier = Modifier) {
Icon(modifier = modifier,
imageVector = if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore,
contentDescription = null,
tint = MaterialTheme.colorScheme.secondary
)
}
@Stable
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBar(modifier: Modifier = Modifier){
CenterAlignedTopAppBar(title = {
Text(text = stringResource(R.string.topBarDescription),
style = MaterialTheme.typography.headlineMedium,
fontSize = 26.sp)
})
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
_30DaysTheme {
_30DaysApp()
}
}
А вот класс, который я использую
Код: Выделить всё
@Immutable
data class Task(
@StringRes val articleRes: Int,
@StringRes val descriptionRes: Int,
@DrawableRes val imageRes: Int,
val id: String = UUID.randomUUID().toString()
)
Я пытался добавить ключи, сжатие изображений, добавить аннотации @Immutable и @Stable, где мог, выпустить сборку и компилятор R8, но он по-прежнему сильно отстает. Любая помощь приветствуется.
Подробнее здесь:
https://stackoverflow.com/questions/783 ... ck-compose
1716672021
Anonymous
Я создал простое 30-дневное приложение, используя lazyColumn, но когда я запускаю его на своем телефоне, оно имеет ОГРОМНЫЕ задержки. [code]class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { _30DaysTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { _30DaysApp() } } } } } @Composable fun _30DaysApp() { Scaffold(topBar = { TopBar() }) { LazyColumn( contentPadding = it, verticalArrangement = Arrangement.spacedBy(12.dp) ) { itemsIndexed(items = tasks, key = { _, task -> task.id }) { index, task -> CardTask(task = task, day = index + 1) } } } } @Composable fun CardTask(task: Task, day: Int, modifier: Modifier = Modifier) { var expand by remember { mutableStateOf(false )} Card(shape = RoundedCornerShape(16.dp), modifier = Modifier .padding(horizontal = 8.dp) .clickable { expand = !expand }) { Column( modifier = modifier .animateContentSize( animationSpec = spring( dampingRatio = Spring.DampingRatioNoBouncy, stiffness = Spring.StiffnessMedium ) ) .padding(12.dp) ) { Row { Text( text = stringResource(R.string.day, day), fontSize = 16.sp, fontWeight = FontWeight.Bold ) Spacer(modifier = modifier.weight(1f)) ExpandButton( expanded = expand, modifier = Modifier.size(24.dp) ) } Text( text = stringResource(task.articleRes), fontSize = 24.sp ) Spacer(modifier = Modifier.size(8.dp)) Image( painter = painterResource(task.imageRes), contentDescription = null, contentScale = ContentScale.FillWidth, modifier = Modifier .fillMaxWidth() .height(160.dp) .clip(RoundedCornerShape(12.dp)), ) if (expand) { Text( text = stringResource(task.descriptionRes), fontFamily = Poppins, modifier = Modifier.padding(top = 12.dp) ) } } } } @Composable fun ExpandButton(expanded: Boolean, modifier: Modifier = Modifier) { Icon(modifier = modifier, imageVector = if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore, contentDescription = null, tint = MaterialTheme.colorScheme.secondary ) } @Stable @OptIn(ExperimentalMaterial3Api::class) @Composable fun TopBar(modifier: Modifier = Modifier){ CenterAlignedTopAppBar(title = { Text(text = stringResource(R.string.topBarDescription), style = MaterialTheme.typography.headlineMedium, fontSize = 26.sp) }) } @Preview(showBackground = true) @Composable fun GreetingPreview() { _30DaysTheme { _30DaysApp() } } [/code] А вот класс, который я использую [code]@Immutable data class Task( @StringRes val articleRes: Int, @StringRes val descriptionRes: Int, @DrawableRes val imageRes: Int, val id: String = UUID.randomUUID().toString() ) [/code] Я пытался добавить ключи, сжатие изображений, добавить аннотации @Immutable и @Stable, где мог, выпустить сборку и компилятор R8, но он по-прежнему сильно отстает. Любая помощь приветствуется. Подробнее здесь: [url]https://stackoverflow.com/questions/78397672/lazycolumn-lags-jetpack-compose[/url]