如何将 'this' 视图传递给 DataBinding?
How to pass 'this' view to DataBinding?
是否可以将 这个 视图传递给数据绑定。
比如我想这样写:
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
bind:imageUrl="@{model.imageUrl(this.width, this.height)}"
/>
您可以传递视图本身的引用,而不是传递 (this.width, this.height)
(将 android:id 提供给您的图像视图)
bind:imageUrl="@{model.imageUrl(idOfImageView}"
并且在您的方法中,您可以从视图本身获取高度和宽度。
假设您有如下所示的自定义 BindingAdapter 方法:
@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView view) {
//Now get image view instance to provide width and height.
}
检查here。
这是@BindingAdapter注解通过数据绑定加载图片的方式
在您的布局标签中添加这一行
xmlns:app="http://schemas.android.com/apk/res-auto"
这是你的 ImageView
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:loadImage="@{imageUrl}"/>
@BindingAdapter({"loadImage"})
public static void loadImage(ImageView imageView, String imageUrl) {
// Load image with your image loading library ,like with Glide or Picasso or any of your favourite
}
是否可以将 这个 视图传递给数据绑定。
比如我想这样写:
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
bind:imageUrl="@{model.imageUrl(this.width, this.height)}"
/>
您可以传递视图本身的引用,而不是传递 (this.width, this.height)
(将 android:id 提供给您的图像视图)
bind:imageUrl="@{model.imageUrl(idOfImageView}"
并且在您的方法中,您可以从视图本身获取高度和宽度。
假设您有如下所示的自定义 BindingAdapter 方法:
@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView view) {
//Now get image view instance to provide width and height.
}
检查here。
这是@BindingAdapter注解通过数据绑定加载图片的方式
在您的布局标签中添加这一行
xmlns:app="http://schemas.android.com/apk/res-auto"
这是你的 ImageView
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:loadImage="@{imageUrl}"/>
@BindingAdapter({"loadImage"})
public static void loadImage(ImageView imageView, String imageUrl) {
// Load image with your image loading library ,like with Glide or Picasso or any of your favourite
}