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

使用Glide无法在GridView中显示图片的技术求助

解决GridView嵌套CardView+Glide加载本地图片空白的问题

我帮你排查了代码里的几个关键问题,逐一修复后应该就能正常显示图片了:

问题1:CardView布局误用了GridLayout专属属性

你的card_view.xml里添加了app:layout_columnSpan、app:layout_rowSpan这类属性——这些是GridLayout子项的专属配置,但你的CardView是作为GridView的子项存在的,这些属性不仅完全无效,还可能干扰控件的布局测量逻辑,导致ImageView无法正常渲染。

问题2:ImageView缺少图片适配配置

默认的ImageView缩放规则可能无法让你的图片填满CardView,看起来就像空白。添加合适的scaleType可以确保图片适配控件尺寸。

问题3:Glide依赖可能未正确添加(隐性问题)

如果项目里没配置Glide的依赖,图片肯定加载失败,这是很容易忽略的点。


修复后的完整代码

1. 修正card_view.xml布局

删除无效属性,添加ImageView的缩放规则:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_margin="3dp"
    app:cardCornerRadius="8dp">

    <ImageView
        android:id="@+id/added_picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" /> <!-- 确保图片填满控件 -->

</androidx.cardview.widget.CardView>

2. 优化Adapter的getView方法(提升性能+容错性)

用ViewHolder模式优化复用逻辑,同时给Glide添加占位图方便排查问题:

class ImageAdapter(private val mContext: Context, private val images: Array<Int>) : BaseAdapter() {

    inner class ViewHolder(val imageView: ImageView)

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val viewHolder: ViewHolder
        val view: View

        if (convertView == null) {
            val layoutInflater = LayoutInflater.from(mContext)
            // 传入parent参数,让布局正确继承GridView的布局参数
            view = layoutInflater.inflate(R.layout.card_view, parent, false)
            viewHolder = ViewHolder(view.findViewById(R.id.added_picture))
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = view.tag as ViewHolder
        }

        // 加载本地drawable,添加占位图方便排查加载异常
        Glide.with(mContext)
            .load(images[position])
            .placeholder(R.drawable.ic_launcher_background) // 加载中的临时图
            .error(R.drawable.ic_launcher_foreground) // 加载失败的提示图
            .into(viewHolder.imageView)

        return view
    }

    override fun getItem(position: Int): Any = images[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getCount(): Int = images.size
}

3. 确保GridView基础配置正确

检查activity_main.xml中的GridView是否有合理的布局参数:

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="110dp"
    android:horizontalSpacing="3dp"
    android:verticalSpacing="3dp" />

4. 添加Glide依赖(如果还没加)

在Module级别的build.gradle中添加依赖:

implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'

完成以上修改后,运行项目应该就能看到CardView里的图片正常显示了。如果还是空白,可以先用系统自带的drawable(比如R.drawable.ic_launcher_background)测试,确认是不是自定义的filled_rectangle资源存在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 17:57:36