-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Data binding support
Since AndroidAnnotations 4.4
You can use data bound layouts in @EActivity
, @EFragment
and @EViewGroup
annotated classes.
To achieve this, you must annotated these classes with the additional @DataBound
annotation.
You have to access the data binding object to bind your data to the UI. To inject the binding object, annotate a non-private field with @BindingObject
. The type of the field must extend ViewDataBinding.
You can first access this field in an @AfterViews
annotated method.
Example:
@DataBound
@EActivity(R.layout.main_activity)
public class MainActivity extends Activity {
@BindingObject
MainActivityBinding binding;
@AfterViews
void bindData() {
binding.setTitle("Your title"); // example binding
}
}
You can use method injection as well for @BindingObject
.
@DataBound
@EActivity(R.layout.main_activity)
public class MainActivity extends Activity {
@BindingObject
void injectBinding(MainActivityBinding binding) {
// assign binding
}
void injectBinding2(@BindingObject MainActivityBinding binding) {
// assign binding
}
}
Since data binding V2, you cannot pass generated View
classes in your binding adapters, but you still have to use your generated views to let AndroidAnnotations work.
There is a workaround: you can have a parent Activity
which inflates the generated views automatically using a little reflection. Subclass that Activity
, and then you can reference the normal (not generated) Views
in your XML layouts and binding adapters.
19/11/2020 The 4.8.0 release is out !
- Get started!
- Cookbook, full of recipes
- Customize annotation processing
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow
- Ask on Gitter