贝利信息

RecyclerView图片加载不显示?可能是这几个原因

日期:2024-11-30 00:00 / 作者:花韻仙語

recyclerview 渲染服务端图片列表不显示的原因

当使用 recyclerview 渲染服务端图片列表时,如果设置 imageview 的高度为 wrap_content,可能会出现图片不显示的问题。这是因为在加载图片时,系统无法确定图片的大小,从而无法精确测量出 imageview 的高度。

解决方案

解决此问题有几种方法:

glide.with(context)
    .load(imageurl)
    .placeholder(r.drawable.placeholder)
    .into(imageview)
Glide.with(context)
    .load(imageUrl)
    .addListener(object : RequestListener {
  

override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { return false } override fun onResourceReady(resource: Drawable?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { // 根据图片宽高比设置 ImageView 的高度 val width = imageView.width val aspectRatio = resource!!.intrinsicWidth.toFloat() / resource.intrinsicHeight.toFloat() val height = Math.round(width / aspectRatio) imageView.layoutParams = ViewGroup.LayoutParams(width, height) return false } }) .into(imageView)