recyclerview 渲染服务端图片不显示的解决方法
在使用 recyclerview 渲染服务端图片列表时,如果不显示图片,可能是因为 imageview 的 layout_height 设置为 wrap_content 导致的。以下是解决方法:
指定一个高度:
t="200dp"使用占位符:
使用 glide 加载图片时,可以使用占位符,例如:
glide.with(this.activity.getbasecontext()) .load(src) .placeholder(r.drawable.placeholder) // 设置占位符 .into(imageview);
动态设置 imageview 高度:
使用 glide 添加requestlistener,当图片加载完成后,根据图片宽高比动态设置 imageview 的高度,例如:
Glide.with(this.activity.getBaseContext())
.load(src)
.addListener(new RequestListener<>() {
@Override
public boolean onResourceReady(Drawable resource, ...) {
float aspectRatio = resource.getIntrinsicWidth() / resource.getIntrinsicHeight();
int height = Math.round(imageView.getWidth() / aspectRatio);
imageView.setLayoutParams(new ViewGroup.LayoutParams(imageView.getWidth(), height));
return false;
}
})
.into(imageView);