简体   繁体   English

使用Glide使用foreach循环显示多幅图像

[英]Show Multiple images using foreach loop using Glide

I have developed a shopping application, the final order placing order i have to show all the cart product details from realm database. 我开发了一个购物应用程序,最终的下订单必须显示领域数据库中的所有购物车产品详细信息。 here i can fetch all the details and working perfect except the product images. 在这里,我可以获取所有详细信息,除了产品图片之外,它们都可以完美地工作。 here i am using enhanced for loop. 在这里,我使用增强的循环。 i am getting exact image URL, but image view not repeating like text view, Please help me, your help will be appreciated. 我正在获取确切的图像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);

        }

of course it will not work as expected, for that, please use recycler view or dynamically generate ImageView s. 当然,它不能按预期工作,为此,请使用recycler view或动态生成ImageView It is impossible to show multiple images in one ImageView through glide 通过滑行无法在一个ImageView中显示多个图像

Here is an example from my util library https://github.com/Link184/Extensions/blob/master/extensions/src/main/java/com/link184/extensions/AndroidExtensions.kt 这是我的实用程序库中的一个示例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)
}

Just call 刚打电话

itemViewsArray.loadUri(cartItems.map { it.toUriFunction() })

The Problem with your code is That you have provided Single ImageView which can hold maximum 1 Image in it. 代码的问题是您提供了单个ImageView,该ImageView中最多可以容纳1张图像。

Why your ImageView shows last Image Only ? 为什么您的ImageView只显示最后一个图像?

Answer : According to your code You setting the image to ImageView. 答案:根据您的代码,将图像设置为ImageView。 It working properly But Problem with it. 它工作正常,但是有问题。 That when the foreach loop ends your imageview get last url of Image from ArrayList. 当foreach循环结束时,您的imageview从ArrayList获取Image的最后一个URL。

So Better Use RecyclerView to show multiple image in list. 因此更好使用RecyclerView在列表中显示多个图像。

For The Reference and Docs you can Click Here . 对于参考和文档,您可以单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM