我已使用以下代码成功构建了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
}
...这是相似的。但这也不起作用。不知道为什么它不起作用...
答案 0 :(得分:0)
在适配器中使用此代码。
Glide.with(context)
.load(url)
.fitCenter()
.centerCrop()
.into(imageview);
答案 1 :(得分:0)
希望它能对您有所帮助。
Glide.with(this).load(recipe).into(row!!.glide_img_cell_id)
答案 2 :(得分:0)
好的,现在解决它:改用Picasso
。做了完全一样的事情。 :/