【问题标题】:Glide not loading image into ImageViewGlide 不将图像加载到 ImageView
【发布时间】:2020-04-01 22:19:15
【问题描述】:

我有一个嵌套在线性布局中的 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>

我还有一个 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()
    }

在我的 RecyclerView 日志中,我可以看到加载到其中的每个模型都有传递给 glide 的 URI,但是图像和进度可绘制对象都没有出现在大约一半的视图中。父母只是折叠和缩小每个 ViewHolder。我的实现有什么问题?

【问题讨论】:

    标签: android xml android-glide


    【解决方案1】:

    要使绑定适配器工作,您必须使用静态函数,在 kotlin 情况下,它将是一个伴随对象并添加 @JvmStatic,如下所示:

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

    【讨论】:

    • 我正在使用这种方法来扩展 ImageView 类。我不认为我可以给它一个伴生对象。我应该为这个方法创建一个单独的类吗?
    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    相关资源
    最近更新 更多