public abstract class DataBinding extends Object
ComponentContainer
and ActiveData objects.
When a ComponentContainer or ActiveData object is modified,
the DataBinding object automatically modifies the object bound to
the ComponentContainer or ActiveData.
For example, if you modified the attribute of a ComponentContainer, DataBinding will synchronize
the attribute value to the ActiveData object bound to the ComponentContainer.
Similarly, if the attribute value of an ActiveData object is changed,
the attribute value of the bound ComponentContainer will also be updated.
"dataBindingEnable true" to the build.gradle
file of the current module as follows:
ohos {
...
buildTypes {
debug {
dataBindingEnabled true
}
...
}
}
Before using the DataBinding class, use an ActiveData object to define the data to observe,
the data type to bind, and the set() and get() methods. The sample code snippet is as follows:
import ohos.aafwk.abilityjet.activedata.ActiveData;
public class FruitComponentModel {
private ActiveData<String> price;
public ActiveData<String> getPrice() {
return price;
}
public void setPrice(ActiveData<String> price) {
this.price = price;
}
}
Then, declare the data to be bound by DataBinding in the layout file. Example declaration:
In this example, a binding is declared in fruit.xml and the system will automatically generate
a FruitBinding class after the compilation.
You can call the DataBindingUtil#createBinding(int, Context, String) method to
obtain a FruitBinding object and call initComponent(ohos.agp.components.ComponentContainer) and
setLifecycle(ohos.aafwk.ability.Lifecycle) to initialize the object.
The obtained FruitBinding object then calls the method defined in FruitComponentModel to
bind it to an ActiveData object. Example code:
public class FruitSlice extends AbilitySlice {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "FruitSlice");
private FruitBinding binding;
@Override
public onStart(Intent intent) {
super.onStart(intent);
ComponentContainer componentContainer =
(ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_fruit, null, false);
if (!(componentContainer instanceof ComponentContainer)) {
return;
}
super.setUIContent(componentContainer);
try {
binding = DataBindingUtil.createBinding(ResourceTable.Layout_fruit, getContext(), "ohos.myapplication");
} catch (IllegalArgumentException | IOException exception) {
HiLog.info(LABEL_LOG, exception.toString());
}
if(binding != null) {
binding.initComponent(componentContainer);
binding.setLifecycle(getLifecycle());
ActiveData<String> price = new ActiveData<>();
price.setData("7.23");
FruitComponentModel apple = new FruitComponentModel();
apple.setPrice(price);
binding.setApple(apple);
}
}
}
ComponentContainer,
ActiveDataSystemCapability.Aafwk#ABILITYJET| 构造器 | 说明 |
|---|---|
DataBinding() |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
Lifecycle |
getLifecycle() |
Obtains the lifecycle of the bound object.
|
abstract void |
initComponent(ComponentContainer componentContainer) |
Initializes the bound components by using
ComponentContainer. |
void |
setLifecycle(Lifecycle lifecycle) |
Sets the lifecycle of the bound object.
|
public void setLifecycle(Lifecycle lifecycle)
The lifecycle of the bound object is required because the data of the object can be updated only when
it is visible (either in the ACTIVE or INACTIVE state).
You are advised to set the lifecycle when initializing the ability slice that hosts the bound object.
lifecycle - Indicates the lifecycle of the bound object.IllegalStateException - Lifecycle can't set twice.public Lifecycle getLifecycle()
You can use Lifecycle to query the lifecycle state of the bound object and listens for
its lifecycle changes.
public abstract void initComponent(ComponentContainer componentContainer)
ComponentContainer.componentContainer - Indicates the group of components to initialize.