DataBinding使用指南(二)@BindingAdapter自定义属性

DataBinding之 自定义属性 Binding adapters


上一章我们主要讲解了简单的文字绑定操作,这一章我们讲一下复杂的数据绑定,例如图片加载。

@BindingAdapter 注解

databinding中自定义属性依赖于注解 @BindingAdapter

  1. 作用于方法(和类无关,这个自定义属性的方法可以写在任何地方)
  2. 它定义了xml的属性赋值的java实现(注解的方法中就是我们对这个控件进行处理)
  3. 方法必须为公共静(public static)方法,可以有一到多个参数。

简单实用

直接上代码

	//“app:imgUrl” 这就是在xml中的属性
    @android.databinding.BindingAdapter("app:imgUrl")
    public static void setImgUrl(ImageView imageView, String url) {
        GlideApp.with(imageView)
                .load(url)
                .into(imageView);
    }

xml 中使用

        <ImageView
            android:id="@+id/img_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:imgUrl="@{user.url}"
            />

多参数使用

继续上代码

    @android.databinding.BindingAdapter(value = {"app:imgUrl", "app:placeholder"}, requireAll = false)
    public static void loadImg(ImageView imageView, String url, Drawable placeholder) {
        GlideApp.with(imageView)
                .load(url)
                .placeholder(placeholder)
                .into(imageView);
    }

xml中代码

        <ImageView
            android:id="@+id/img_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:imgUrl="@{user.url}"
            app:placeholder="@{@drawable/ic_launcher_background}"
            />

这里 requireAll = false 表示我们可以使用这两个两个属性中的任一个或同时使用,如果requireAll = true 则两个属性必须同时使用,不然报错。默认为 true。
xml 中@{@drawable/ic_launcher_background} 引用的是资源文件中的图片

使用属性旧值

@BindingAdapter 自定义属性可以使用属性旧值,即上一次设置的属性值
上代码

   @android.databinding.BindingAdapter(value = {"app:imgUrl", "app:placeholder"}, requireAll = false)
   public static void loadImg(ImageView imageView, String oldUrl, Drawable oldError, String newUrl, Drawable newError) {
       GlideApp.with(imageView)
               .load(newUrl)
               .placeholder(oldError)
               .into(imageView);
   }

这里要注意一点:如果是多个属性,那么方法的参数必须要把所有的属性的旧值列举出来,然后在列举属性新值。这个顺序是不能乱的。并不是一个属性旧值跟一个属性新值。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以通过在自定义控件的布局文件中使用 `<layout>` 标签来启用 DataBinding,然后在代码中使用 DataBindingUtil 类来绑定数据。 例如,假设我们有一个自定义控件 MyCustomView,它的布局文件为 custom_view.xml,我们想要绑定一个名为 `text` 的字符串属性。我们可以这样做: 1. 在 custom_view.xml 中使用 `<layout>` 标签包裹布局文件的根布局: ```xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 自定义控件的布局 --> </LinearLayout> </layout> ``` 2. 在 MyCustomView 的构造函数中使用 DataBindingUtil.inflate 方法来获取绑定对象,并将它与自定义控件的根布局绑定: ```java public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); // 获取绑定对象 CustomViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_view, this, true); // 绑定数据 binding.setText("Hello, world!"); } ``` 3. 在 MyCustomView 中添加一个 `text` 属性,并在 custom_view.xml 中使用 `@{}` 语法来绑定该属性: ```java public class MyCustomView extends LinearLayout { private String text; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); CustomViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_view, this, true); binding.setCustomView(this); } public String getText() { return text; } public void setText(String text) { this.text = text; } } ``` ```xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="customView" type="com.example.MyCustomView" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{customView.text}" /> </LinearLayout> </layout> ``` 这样,当 MyCustomView 的 `text` 属性发生变化时,custom_view.xml 中的 TextView 的文本也会自动更新。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值