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<Drawable> { override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean { return false } override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, 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)
以上就是RecyclerView图片加载不显示?可能是这几个原因的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号