我有一个嵌套在线性布局中的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日志中,我可以看到加载到其中的每个模型都有正在传递的URI,但是在大约一半的视图中都没有出现图像或可绘制进度。父母只是在缩小和缩小每个ViewHolder。我的实现有什么问题?
答案 0 :(得分:0)
要使绑定适配器正常工作,您必须使用静态功能,在kotlin的情况下,它将是一个伴随对象并添加@JvmStatic,如下所示:
companion object{
@BindingAdapter("android:imageUrl")
@JvmStatic
fun loadImages(view: ImageView, url: String){
view.loadImage(url, getProgressDrawable(view.context))
}
}