After reviewing Data Binding documentation on the difference between <import/>
and <variable/>
, it's unclear how they differ. Below are examples taken from the documentation page.
<import type="com.example.real.estate.View" alias="Vista"/>
looks like the equivalent of
<variable name="user" type="com.example.User"/>
except that an alias
can start with a capital letter, while name
cannot. They are even used similarly.
<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"/>
The only difference I'm seeing, from the examples, are that you can call an import
's methods, but not a variable
's.
You use variable
if you want to pass some data to views. In your example, you have user variable which is of type User and you use it to set users name in TextView
. You can call variables methods - user.lastName
is equivalent to user.getLastName()
With import
you only specify class that you want to use, you don't pass any data. In your example, the imported utility class is used only to capitalize users name, which the capitalize
method receives as an argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With