[英]Show Multiple images using foreach loop using Glide
我开发了一个购物应用程序,最终的下订单必须显示领域数据库中的所有购物车产品详细信息。 在这里,我可以获取所有详细信息,除了产品图片之外,它们都可以完美地工作。 在这里,我使用增强的循环。 我正在获取确切的图像URL,但是图像视图不会像文本视图那样重复出现,请帮助我,我们将不胜感激。
void productname(){
pname = "";
pimage= "";
for (Cart cart : cartItems) {
pname = pname + cart.getName() + "!";
pname = pname.replaceAll("!", "\n\n");
pimage=pimage+Constants.getProductBaseUrlThumb(getContext()) + cart.getImage();
Glide.with(getContext())
.load(pimage)
.asBitmap()
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(ivProductImage);
}
当然,它不能按预期工作,为此,请使用recycler view
或动态生成ImageView
。 通过滑行无法在一个ImageView中显示多个图像
这是我的实用程序库中的一个示例https://github.com/Link184/Extensions/blob/master/extensions/src/main/java/com/link184/extensions/AndroidExtensions.kt
/**
* [ImageView] and url will be grouped consecutively. Take care to pass all urls and ImageViews
* in the same order.
* @throws IllegalStateException when image view array size is different from url array size
*/
fun Array<ImageView>.loadUri(vararg uri: Uri, block: GlideRequest<Drawable>.() -> GlideRequest<Drawable> = { this }) {
if (size != uri.size) {
throw IllegalStateException("Image view and image url count do not correspond.")
}
forEachIndexed { index, it ->
it.loadUri(uri[index], block)
}
}
inline fun ImageView.loadUri(uri: Uri, block: GlideRequest<Drawable>.() -> GlideRequest<Drawable> = { this }) {
block(GlideApp.with(this).load(uri)).into(this)
}
刚打电话
itemViewsArray.loadUri(cartItems.map { it.toUriFunction() })
代码的问题是您提供了单个ImageView,该ImageView中最多可以容纳1张图像。
为什么您的ImageView只显示最后一个图像?
答案:根据您的代码,将图像设置为ImageView。 它工作正常,但是有问题。 当foreach循环结束时,您的imageview从ArrayList获取Image的最后一个URL。
因此更好使用RecyclerView
在列表中显示多个图像。
对于参考和文档,您可以单击此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.