lilypuchi
10/19/2022, 10:16 PMIconButton
in a list item of a LazyColumn
to which I pass Icon
in two ways:
1. using painterResource()
with my in app Image drawables (vector)
Icon(
modifier = Modifier.size(16.dp),
painter = painterResource(
id = if (isLiked) R.drawable.ic_like_filled else R.drawable.ic_like
),
contentDescription = null,
tint = if (isLiked) AppTheme.colors.selected else AppTheme.colors.unselected
)
This leads to Recomposition of Icon.
2. using imageVector
with MaterialIcons
Icon(
modifier = Modifier.size(16.dp),
imageVector = if (isLiked) Icons.Rounded.Favorite else Icons.Rounded.FavoriteBorder,
contentDescription = null,
tint = if (isLiked) AppTheme.colors.selected else AppTheme.colors.unselected
)
This skips Recomposition of Icon.
Internally both seem to using rememberVectorPainter()
and I’m failing to see why painterResource
internal implementation can cause a Recomposition. Am I missing something here? 🤔
(Attaching the recomposition counter screenshots for reference. Attached in order of the above cases)lilypuchi
10/19/2022, 10:18 PM@Composable
fun TalkLikeCount(
modifier: Modifier = Modifier,
likesCount: Int,
isLiked: Boolean = false,
onLikeClicked: () -> Unit
) {
IconButton(
modifier = modifier,
onClick = onLikeClicked
) {
Row {
Icon(..)
Text(text = "$likesCount")
}
}
}
Pedro Alberto
10/20/2022, 6:36 AMval ResId = if (isLiked) R.drawable.ic_like_filled else R.drawable.ic_like
Icon(ImageVector.vectorResource(id = ResId), contentDescription = "")
and let me know ?MR3Y
10/20/2022, 6:52 AMlilypuchi
10/20/2022, 6:54 AM