You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Landscapist库GlideImage缺失loading等参数,编译报错求解决方案

解决Landscapist GlideImage缺失loading/success/failure参数的编译错误

核心原因

你遇到的问题大概率是依赖版本不匹配:GitHub文档里的loading/success/failure参数是Landscapist较新版本才新增的API,如果你项目中引入的是旧版本依赖,Android Studio自然会抛出编译错误。

解决方案

1. 升级依赖版本

检查项目的build.gradle(Module级别)中Landscapist Glide的依赖,替换为最新稳定版:

implementation "com.github.skydoves:landscapist-glide:2.2.0" // 请替换为当前最新版本

升级后,你提供的代码示例就能正常编译使用了。

2. 旧版本替代方案(无法升级时)

如果因为项目限制不能升级依赖,可以通过手动管理图片加载状态实现相同效果:

val imageState = rememberGlideImageState(imageModel = imageUrl)

when (imageState) {
    is GlideImageState.Loading -> {
        Box(modifier = Modifier.matchParentSize()) {
            CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
        }
    }
    is GlideImageState.Success -> {
        Image(
            painter = imageState.painter,
            contentDescription = null,
            modifier = modifier
        )
    }
    is GlideImageState.Failure -> {
        // 自定义加载失败时的UI,比如显示错误图标
        Icon(Icons.Default.Error, contentDescription = "加载失败")
    }
}

另外,旧版本也支持通过placeholder和error参数快速设置占位和错误UI:

GlideImage(
    imageModel = imageUrl,
    modifier = modifier,
    placeholder = {
        CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
    },
    error = {
        Icon(Icons.Default.Error, contentDescription = "加载失败")
    }
)

内容的提问来源于stack exchange,提问作者Raj Narayanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 14:15:41