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

如何在Kotlin的RecyclerView中展示网站的多张图片

解决方法

1. 用图片加载库实现网络图片加载(推荐)

Kotlin开发中不会手动写原生请求拉取图片,成熟的图片加载库能直接从URL加载图片到ImageView,还自带缓存、RecyclerView适配优化,完全满足你批量展示图片的需求,常用的有Coil(Kotlin专属轻量库)、Glide:

Coil示例

  • 先在Module级别的build.gradle中添加依赖:
implementation("io.coil-kt:coil:2.4.0")
  • 在RecyclerView的Adapter的onBindViewHolder方法里直接加载:
// 先给你的PostModel补充图片URL字段(原数据类缺少该信息,必须先获取到每个应用的图片地址)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val item = list[position]
    holder.imageView.load(item.imageUrl) {
        placeholder(R.drawable.loading_placeholder) // 加载中占位图
        error(R.drawable.load_error) // 加载失败占位图
    }
}

Glide示例

  • 添加依赖:
implementation("com.github.bumptech.glide:glide:4.16.0")
annotationProcessor("com.github.bumptech.glide:compiler:4.16.0")
  • Adapter中加载逻辑:
Glide.with(holder.imageView.context)
    .load(item.imageUrl)
    .placeholder(R.drawable.loading_placeholder)
    .error(R.drawable.load_error)
    .into(holder.imageView)

2. 批量获取图片URL的注意事项

你提到想批量获取主URL下的所有图片,需要先明确:

  • 优先找网站公开的API接口,一般这类应用列表网站会有返回所有应用信息(包含图片URL)的接口,这是最合规高效的方式;
  • 你粘贴的@font-face代码是字体加载样式,和图片无关,属于误粘贴,先排查到正确的图片URL格式;
  • 如果没有公开API,网页爬取需遵守网站的robots协议和相关法律法规。

3. 补充你的数据类

原PostModel缺少图片URL字段,需要扩展:

data class PostModel(
    val role : String = "listitem",
    val clazz : String = "Zc7IjY",
    val imageUrl: String // 新增图片URL字段,用于加载图片
) 

内容的提问来源于stack exchange,提问作者KeremE.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 21:01:02