Android 数据绑定(bind)以编程方式实例化 View

标签 android android-databinding

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

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

class MyViewModel {
}

class MyCustomView extends View {
}

class MyAnotherCustomView extends MyCustomView {
}

现在假设我使用以下方法实例化 MyCustomView/MyAnotherCustomView:

MyCustomView customView = new MyCustomView(context);

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

我的第二个问题是第一个问题的跟进。可以说在我的第一个问题中不可能实现我想要的。然后,我必须定义一个 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 的一个子类,保持绑定(bind)逻辑不变,我是否必须创建一个新的 xml 文件 my_another_custom_view.xml 只是将 MyCustomView 替换为 MyAnotherCustomView定义相同的绑定(bind)?

最佳答案

第一个问题的答案是“否”。 Android 数据绑定(bind)需要 XML 来生成绑定(bind)类。

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

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

在这里,我假设选择哪个自定义 View 是由字段 layoutId 确定的。每个可能的布局都必须定义一个 User 类型的 user 变量。

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

<?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>

关于Android 数据绑定(bind)以编程方式实例化 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917521/

相关文章:

android - 如何使用 Kivy 检测操作系统/外围设备?

android - 致命异常 : java. lang.NoClassDefFoundError

Android自定义 View : How to update custom enum attribute via LiveData & data binding

android - 数据绑定(bind)和包含的布局 : Cannot find setter attribute for onClick

java - Android布局-设置相对于屏幕尺寸的圆角半径

android - 在 android 中不使用 findViewById() 查找所有微调器 View

android - 如何在Android中获取ObservableField的值

android - 如何使用 ObservableField

android - 使用 DataBinding 在 RecyclerView 项目中绑定(bind)图像时闪烁图像

android - 如何使用同一帐户在 ios 和 android 上启用 facebook 登录