在检查<import/>
和<variable/>
之间的区别之后,Data Binding documentation尚不清楚它们之间的区别。以下是从文档页面获取的示例。
<import type="com.example.real.estate.View" alias="Vista"/>
看起来像
<variable name="user" type="com.example.User"/>
除了alias
可以以大写字母开头,而name
不能以大写字母开头。它们甚至被类似地使用。
<data>
<import type="com.example.MyStringUtils"/>
<variable name="user" type="com.example.User"/>
</data>
…
<TextView
android:text="@{MyStringUtils.capitalize(user.lastName)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
从示例中看到的唯一区别是,您可以调用import
的方法,而不能调用variable
的方法。
答案 0 :(得分:1)
如果要将某些数据传递给视图,请使用variable
。在您的示例中,您有一个用户类型为User的用户变量,并使用它在TextView
中设置了用户名。您可以调用变量方法-user.lastName
等效于user.getLastName()
使用import
仅指定您要使用的类,而不会传递任何数据。在您的示例中,导入的实用程序类仅用于大写用户名,capitalize
方法将其用作参数。