In the latest Android project 8-Data Binding advanced article, we mentioned the use of Component for injection to facilitate testing. Some friends said that it was not clear enough, so we will discuss Component in detail.

As an example, our implementation goal is to use the Data Binding Component to make the text of the applied global TextView become test at any time and also to perform global peels.

The code is in the Component package in DataBindingSample.

DataBindingComponent interface

In the build/intermediates/classes below, you can find the DataBindingComponent class, package, called android. The databinding, global will only have a class – this interface generated at compile time, Contains getter methods for all instances of BindingAdapters used.

When a BindingAdapter is an instance method, an instance of the class that implements the method must be instantiated. The generated interface contains the GET method for each class/interface that declares a BindingAdapter. Naming conflicts are resolved by simply adding a number prefix to the GET method.

With the Dagger 2, the developer can inherit this interface and annotate the inherited interface as Component.

Corresponding interfaces are as follows:

  • setDefaultComponent(DataBindingComponent))

  • Inflate (LayoutInflater, int, ViewGroup, Boolean, DataBindingComponent)), a binding layout is synchronized and returns the newly created binding

  • Bind (View, DataBindingComponent)), returns a binding based on the given layout root, or creates one if none

The first interface works globally, and the last two only work on the layout of the statement inflate.

Create a Component

Declare abstract Adapter

If you don’t need to implement multiple Components, you can skip this step.

We declare an abstract Adapter in which we write abstract methods to set the properties we want to make data Binding. Here we directly eliminate the text and textColor properties in the Android namespace of the TextView.

The @bindingAdapter annotation here tells the Data Binding to generate our Adapter’s GET method (which must be non-static) in the Component.

Review images

Realize the adapter

We inherit MyBindingAdapter and implement two adapters respectively:

ProductionBindingAdapter.java:

Review images

TestBindingAdapter.java:

Review images

The former uses the original Settings, while the latter adds the suffix “test” to the text and converts the color to achieve the “skin” function of the font color.

To implement component

After writing the above code, take a look at the DataBindingComponent and see that it has an additional interface method. Implement it:

Production environment Component:

Review images

Test environment Component:

Review images

use

layout

The original text and textColor properties are not set with a data Binding, so we’ll put @{} on them:

Review images

Injection component

Injecting Component is easy. We do global injection by calling:

Review images

Recreate the activity

Since the click event is triggered after the MainActivity is created, it doesn’t work on this activity and we need to recreate it:

Review images

Set the following set (). Click the last button and the font color changes. The text of the TextView is followed by the test string.

Static Adapter method

How does a static BindingAdapter method associate with Component? It’s as simple as taking the first argument to the method:

Review images

In this article we experimented with one of the more advanced features of the Data Binding: Component.

There are many usage scenarios, such as:

  • In the skin

  • dot

  • Replace native properties

  • , etc.

You are welcome to use your imagination to add more scenarios.