To better understand the @bindingAdapter, github provides a adapters directory for you to check.

review

What did we talk about in the first two lectures?

  • MVVM model concepts
  • What is the dataBinding
  • Demonstrates how XML becomes code
  • Demonstrates data automatically notifying a BaseObservable

From the above, we learned how XML becomes code and data (Model) and View are associated with dataBinding. After all, the system provides a limited number of adapters (@bindingAdapter annotation static methods). Can we define our own methods for attributes?

The answer is YES. So we need to write our own @BindingAdapter to do what we need. In this way, we will find the dataBinding outstanding advantage.

So, let’s look at the steps:

1 plus a custom property

Review images

  • Add an age attribute with an integer tag type

2 is used in our XML

Review images

  • Add the attribute value associated with the age in User. (Because it is our custom attribute, the system must not know what the corresponding function method is, so the @bindingAdapter to specify the direction)

3 Adaptation Method

Review images

  • In a random location, write the following code: static function must be used, for easy reasons. (It’s weird to use an instance that is not static.)
  • Two arguments, the first one is where we’re using it, because we’re using it in a TextView, so the first argument is a TextView type. The second one is the type we defined, int, and we’ll just do it.

4 the demonstration

In our User class, write:

A value age was added and marked as observable

Review images

Implement a setup interface. Review images

Call place:

Review images

We can see that when the age is assigned, the interface displays correctly. The effect is as follows:

Review images

5 afterword.

  • Let’s take a look at what the @BindingAdapter finally holds in the ActivityMainBinding: View images
  • There you go. What is the first element when we use tools? We think about what problems we’re going to encounter, and then we can find solutions. We’re going to use our actual practice to implement this dataBinding, and then we’re going to quickly understand the nature of this thing.

6 Code Location

Github.com/luxiaoming/…

Review images