Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Lambda expressions


Lambda expressions


You can write short callbacks directly in your layout file by using lambda expressions. These are
simplified versions of Java’s lambda expressions:


<Button
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="@{viewModel.title}"
android:onClick="@{(view) -> viewModel.onButtonClick()}"
tools:text="Sound name"/>


Like Java 8 lambda expressions, these are turned into implementations for the interface you use them
for. (In this case, View.OnClickListener.) Unlike Java 8 lambda expressions, these expressions must
use this exact syntax: The parameter must be in parentheses, and you must have exactly one expression
on the right-hand side.


Also, unlike in Java lambdas, you can omit the lambda parameters if you are not using them. So this
works fine, too:


android:onClick="@{() -> viewModel.onButtonClick()}"


More syntactic sugar


You also get a few additional bits of handy syntax for data binding. Particularly handy is the ability to
use backticks for double quotes:


android:text="@{File name: + viewModel.title}"


Here, File name means the same thing as "File name".


Binding expressions also have a null coalescing operator:


android:text="@{File name: + viewModel.title ?? No file}"


In the event that title is null, the ?? operator will yield the value "No file" instead.


In addition, there is automatic null handling provided for you in data binding. Even if viewModel is
null in the code above, data binding will provide appropriate null checks so that it does not crash the
app. Instead of crashing, the subexpression viewModel.title will yield "null".


BindingAdapters


By default, data binding interprets a binding expression as a property invocation. So the following:


android:text="@{File name: + viewModel.title ?? No file}"


is translated into an invocation of the setText(String) method.


Sometimes, though, that is not enough, and you want some custom behavior to be applied for a
particular attribute. In those cases, you can write a BindingAdapter:

Free download pdf