Android 数据绑定以编程方式实例化视图

Android Data Binding Programmatically Instantiated View

Android 文档很好地描述了如何使用布局 xml 文件创建绑定 class。但我有几个问题。

有没有办法为以编程方式实例化的自定义视图创建数据绑定 class?例如,假设我有两个自定义视图 class,我想以编程方式将相同的视图模型对象绑定到它们,而不使用任何 xml。 class如下:

class MyViewModel {
}

class MyCustomView extends View {
}

class MyAnotherCustomView extends MyCustomView {
}

现在假设我实例化 MyCustomView/MyAnotherCustomView 使用:

MyCustomView customView = new MyCustomView(context);

在这种情况下如何使用数据绑定?这可能使用官方 Android 数据绑定框架吗?如果没有,还有哪些其他 frameworks/libraries 可用或推荐来实现此目的?

我的第二个问题是第一个问题的后续。可以说在我的第一个问题中不可能实现我想要的。然后,我将不得不定义一个 my_custom_view.xml 文件。这看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <com.example.name.MyCustomView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="@{user.firstName}"/>
</layout>

现在,如果我想使用 MyAnotherCustomView,它是 MyCustomView 的子class,保持绑定逻辑不变,我是否必须创建一个新的 xml 文件 my_another_custom_view.xml将 MyCustomView 替换为 MyAnotherCustomView 以定义相同的绑定?

第一个问题的答案是 "No." Android 数据绑定需要 XML 生成绑定 classes。

在你的第二个问题中,你提供了一个可行的解决方案。如果你走那条路,一种方法是使用 ViewDataBinding base class setters 来设置你的变量。我可以想象这样的方法:

public void addCustomView(LayoutInflater inflater, ViewGroup container, User user) {
    ViewDataBinding binding = DataBindingUtil.inflate(inflater,
        this.layoutId, container, true);
    binding.setVariable(BR.user, user);
}

在这里,我假设选择哪个自定义视图是由一个字段layoutId决定的。每个可能的布局都必须定义类型为 Useruser 变量。

我不知道你的使用细节,但如果你想动态选择加载哪个自定义视图,你可以使用 ViewStub。如果您在加载自定义视图时没有任何巨大的开销,您也可以只使用可见性来做同样的事情。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
   <data>
       <import type="android.view.View"/>
       <variable name="user" type="com.example.User"/>
       <variable name="viewChoice" type="int"/>
   </data>
   <FrameLayout ...>
       <!-- All of your outer layout, which may include binding
            to the user variable -->
       <ViewStub android:layout="@layout/myCustomView1"
                 app:user="@{user}"
                 android:visiblity="@{viewChoice == 1} ? View.VISIBLE : View.GONE"/>
       <ViewStub android:layout="@layout/myCustomView2"
                 app:user="@{user}"
                 android:visiblity="@{viewChoice == 2} ? View.VISIBLE : View.GONE"/>
    </FrameLayout>
</layout>