简体   繁体   English

Glide 不加载图像到 ImageView

[英]Glide not loading image into ImageView

I have an ImageView nested inside a linear layout:我有一个嵌套在线性布局中的 ImageView:

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/tile_background"
        android:elevation="2dp"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/coverTile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/image_frame_community"
            android:imageUrl="@{sponsorship.coverTileUri}">

        </ImageView>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:layout_marginTop="-7dp"
            android:layout_marginBottom="5dp"
            android:background="@color/white"
            android:paddingLeft="20dp">

            <TextView
                android:id="@+id/postActionText"
                style="@style/ActionFont"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                tools:text="@string/watch_respond">

            </TextView>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@drawable/ic_chevron_right_black_36dp">

            </ImageView>


        </RelativeLayout>


    </LinearLayout>

I also have an extension function for the ImageView that uses Glide and is bound to the layout with databinding:我还有一个 ImageView 的扩展函数,它使用 Glide 并通过数据绑定绑定到布局:

    fun ImageView.loadImage(uri: String, progressDrawable: CircularProgressDrawable) {

    val options = RequestOptions().placeholder(progressDrawable).error(R.drawable.ic_launcher_background)

    this.clipToOutline = true

    Glide.with(context)
        .setDefaultRequestOptions(options)
        .load(uri).into(this)


}

@BindingAdapter("android:imageUrl")
fun loadImage(view: ImageView, url: String) {
    view.loadImage(url, getProgressDrawable(view.context))
}




fun getProgressDrawable(context: Context): CircularProgressDrawable {
    return CircularProgressDrawable(context).apply {
        strokeWidth = 10f
        centerRadius = 50f
        start()
    }

In my RecyclerView logs I can see that every model loaded into it has the URI that is being passed into glide, but neither the image nor the progress drawable is appearing in about half of the Views.在我的 RecyclerView 日志中,我可以看到加载到其中的每个模型都有被传递到 glide 的 URI,但是图像和进度绘制都没有出现在大约一半的视图中。 The parents are just collapsing and shrinking each ViewHolder.父母只是折叠和缩小每个 ViewHolder。 What is wrong with my implementation?我的实现有什么问题?

for the binding adapter to work you must work on static function, in kotlin case it would be a companion object and add @JvmStatic, like this:要使绑定适配器工作,您必须处理静态函数,在 kotlin 情况下,它将是一个伴随对象并添加 @JvmStatic,如下所示:

companion object{
@BindingAdapter("android:imageUrl")
@JvmStatic
    fun loadImages(view: ImageView, url: String){
        view.loadImage(url, getProgressDrawable(view.context))
    }
}

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

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