glide
glide copied to clipboard
Use mutableStateMapOf as the data source to fill lazyrow. Glide will not update the picture after updating the duplicate data.
trafficstars
I use glideCompose1.0.0-beta01 When I use mutableStateMapOf as the data source to populate lazyRow, using repeated image paths does not seem to trigger an update. Below is my code
val pagMediaMap = remember { mutableStateMapOf<Int, String>() }
val currPagImageIndex = remember { mutableIntStateOf(0) }
TempSelectPreview(pagFile, pagMediaMap, currPagImageIndex)
@OptIn(ExperimentalGlideComposeApi::class)
@Composable
@Preview
private fun TempSelectPreview(
pagFile: PAGFile? = null,
pagMediaMap: SnapshotStateMap<Int, String> = mutableStateMapOf(),
currPagImageIndex: MutableIntState = mutableIntStateOf(0)
) {
if (pagFile == null) return
LazyRow(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 12.dp)
.height(50.dp),
contentPadding = PaddingValues(horizontal = 12.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp, Alignment.CenterHorizontally)
) {
for (i in 0 until pagFile.numImages()) {
item(key = i) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(12.dpF))
.fillMaxHeight()
.aspectRatio(1F)
.antiShakeClick {
currPagImageIndex.intValue = i
}
) {
if (pagMediaMap[i] == null || pagMediaMap[i].isNullOrBlank()) {
Canvas(modifier = Modifier.fillMaxSize()) {
drawRect(Color.White.setAlpha(0.1F))
if (currPagImageIndex.intValue == i) {
drawRect(CustomColor.DarkBtnBGLEVEL2.setAlpha(0.1F))
}
}
} else {
GlideImage(
model = pagMediaMap[i],
contentDescription = "",
alignment = Alignment.Center,
contentScale = ContentScale.Crop,
transition = CrossFade
)
}
Canvas(modifier = Modifier.fillMaxSize()) {
if (currPagImageIndex.intValue == i) {
drawRoundRect(
CustomColor.DarkBtnBGLEVEL2.setAlpha(0.4F),
cornerRadius = CornerRadius(12.dpF, 12.dpF),
style = Stroke(width = 4.dpF)
)
}
}
}
}
}
}
}
it s select image code
@OptIn(ExperimentalGlideComposeApi::class)
@Composable
@Preview
private fun MediaList(
mediaList: State<List<MediaSelectBean>?>? = null,
lazyGridState: LazyGridState = rememberLazyGridState(),
pagMediaMap: SnapshotStateMap<Int, String> = mutableStateMapOf(),
currPagImageIndex: MutableIntState = mutableIntStateOf(0)
) {
val navPadding = WindowInsets.systemBars.asPaddingValues().calculateBottomPadding()
LazyVerticalGrid(
columns = GridCells.Fixed(4), state = lazyGridState,
contentPadding = PaddingValues(start = 12.dp, end = 12.dp, bottom = navPadding),
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalArrangement = Arrangement.spacedBy(6.dp)
) {
mediaList?.value?.let { list ->
itemsIndexed(list, key = { index, _ -> index }) { index, item ->
Box(
modifier = Modifier
.clip(shape = RoundedCornerShape(12.dp))
.fillMaxWidth()
.aspectRatio(1F)
.antiShakeClick {
pagMediaMap[currPagImageIndex.intValue] = item.mediaPath
}
) {
GlideImage(
model = item.mediaPath,
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
modifier = Modifier.fillMaxSize(),
transition = CrossFade,
contentDescription = ""
)
}
}
}
}
}
https://github.com/bumptech/glide/assets/44636532/7427c697-ecd7-4ff3-8b41-b70fedcc7d52