如何在GridView中使用Glide? (机器人/科特林)

问题描述 投票:0回答:3

我使用以下代码成功构建了gridView

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

    var inflater = mContext!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var row = inflater.inflate(R.layout.image_cell, null)  

    row!!.glide_img_cell_id.setImageResource(R.drawable.cookie_icon_60)

    return row
}

它返回测试图像,因为它应该在gridView但是,我的真实图像数据来自urls所以我使用Glide。我已经使用Glide检查了我的真实数据,它运行正常。但是,我似乎无法让它在我的GridView中工作。这是我改变的代码:

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

    var recipe = this.recipeArray!![position].recipeImage
    var inflater = mContext!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var row = inflater.inflate(R.layout.image_cell, null)  

    row!!.glide_img_cell_id.loadImageUrl(recipe) as GlideImageView

    return row
}

我做同样的事情,除了我现在使用Glide。另外,我很困惑的另一件事是convertView。我看过的一些教程使用它,其他教程没有。它是什么?显然我在第一个例子中并不需要它,因为它有效。

更新投射到GlideImageView应用程序崩溃与kotlin.Unit cannot be cast to com.bumptech.glide.Glide,没有铸造它根本没有显示任何东西。

UPDATE2正如所建议的,我将代码更改为以下内容,但应用程序仍为空白且未显示任何内容:

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var recipe = this.recipeArray!![position].recipeImage

    var inflater = mContext!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var row = inflater.inflate(R.layout.image_cell, null)  
    Glide.with(mContext).load(recipe).fitCenter().centerCrop().into(row!!.glide_img_cell_id)

    return row
}

这看起来是对的吗?

更新仍然没有解决这个问题。查看了应该包含图像的xml文件。看起来像这样:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/glide_img_cell_id"
    android:layout_centerInParent="true"
    android:scaleType="centerCrop"
    />

也使用<com.master.glideimageview.GlideImageView而不是<ImageView但仍然没有。

也试过这个:

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var recipe = this.recipeArray!![position].recipeImage

    var cv = convertView
    var test = LayoutInflater.from(mContext)
    cv = test.inflate(R.layout.image_cell, parent, false)

    Glide.with(this.mContext).load(recipe).into(cv!!.glide_img_cell_id as ImageView)

    return cv
}

......这是相似的。但这也不起作用。不知道为什么它不起作用......

android gridview kotlin android-glide
3个回答
0
投票

好的,现在解决了:改用Picasso。完全相同的事情。 :/


0
投票

我希望它对你有所帮助。

Glide.with(this).load(recipe).into(row!!.glide_img_cell_id)

0
投票

在适配器中使用此代码。

Glide.with(context)
                .load(url)
                .fitCenter()
                .centerCrop()
                .into(imageview);
© www.soinside.com 2019 - 2024. All rights reserved.