public class DataBindingUtil extends Object
ViewDataBinding
from layouts.Modifier and Type | Method and Description |
---|---|
static <T extends ViewDataBinding> |
bind(View root)
Returns the binding for the given layout root or creates a binding if one
does not exist.
|
static <T extends ViewDataBinding> |
bind(View root,
DataBindingComponent bindingComponent)
Returns the binding for the given layout root or creates a binding if one
does not exist.
|
static String |
convertBrIdToString(int id)
Converts the given BR id to its string representation which might be useful for logging
purposes.
|
static <T extends ViewDataBinding> |
findBinding(View view)
Retrieves the binding responsible for the given View.
|
static <T extends ViewDataBinding> |
getBinding(View view)
Retrieves the binding responsible for the given View layout root.
|
static DataBindingComponent |
getDefaultComponent()
Returns the default
DataBindingComponent used in data binding. |
static <T extends ViewDataBinding> |
inflate(LayoutInflater inflater,
int layoutId,
ViewGroup parent,
boolean attachToParent)
Inflates a binding layout and returns the newly-created binding for that layout.
|
static <T extends ViewDataBinding> |
inflate(LayoutInflater inflater,
int layoutId,
ViewGroup parent,
boolean attachToParent,
DataBindingComponent bindingComponent)
Inflates a binding layout and returns the newly-created binding for that layout.
|
static <T extends ViewDataBinding> |
setContentView(Activity activity,
int layoutId)
Set the Activity's content view to the given layout and return the associated binding.
|
static <T extends ViewDataBinding> |
setContentView(Activity activity,
int layoutId,
DataBindingComponent bindingComponent)
Set the Activity's content view to the given layout and return the associated binding.
|
static void |
setDefaultComponent(DataBindingComponent bindingComponent)
Set the default
DataBindingComponent to use for data binding. |
public static void setDefaultComponent(DataBindingComponent bindingComponent)
DataBindingComponent
to use for data binding.
bindingComponent
may be passed as the first parameter of binding adapters.
When instance method BindingAdapters are used, the class instance for the binding adapter is retrieved from the DataBindingComponent.
public static DataBindingComponent getDefaultComponent()
DataBindingComponent
used in data binding. This can be
null
if no default was set in
setDefaultComponent(DataBindingComponent)
.DataBindingComponent
used in data binding. This can be
null
if no default was set in
setDefaultComponent(DataBindingComponent)
.public static <T extends ViewDataBinding> T inflate(LayoutInflater inflater, int layoutId, @Nullable ViewGroup parent, boolean attachToParent)
setDefaultComponent(DataBindingComponent)
.
Use this version only if layoutId
is unknown in advance. Otherwise, use
the generated Binding's inflate method to ensure type-safe inflation.
inflater
- The LayoutInflater used to inflate the binding layout.layoutId
- The layout resource ID of the layout to inflate.parent
- Optional view to be the parent of the generated hierarchy
(if attachToParent is true), or else simply an object that provides
a set of LayoutParams values for root of the returned hierarchy
(if attachToParent is false.)attachToParent
- Whether the inflated hierarchy should be attached to the
parent parameter. If false, parent is only used to create
the correct subclass of LayoutParams for the root view in the XML.null
if
the layoutId wasn't for a binding layout.InflateException
- When a merge layout was used and attachToParent was false.setDefaultComponent(DataBindingComponent)
public static <T extends ViewDataBinding> T inflate(LayoutInflater inflater, int layoutId, @Nullable ViewGroup parent, boolean attachToParent, DataBindingComponent bindingComponent)
Use this version only if layoutId
is unknown in advance. Otherwise, use
the generated Binding's inflate method to ensure type-safe inflation.
inflater
- The LayoutInflater used to inflate the binding layout.layoutId
- The layout resource ID of the layout to inflate.parent
- Optional view to be the parent of the generated hierarchy
(if attachToParent is true), or else simply an object that provides
a set of LayoutParams values for root of the returned hierarchy
(if attachToParent is false.)attachToParent
- Whether the inflated hierarchy should be attached to the
parent parameter. If false, parent is only used to create
the correct subclass of LayoutParams for the root view in the XML.bindingComponent
- The DataBindingComponent to use in the binding.null
if
the layoutId wasn't for a binding layout.InflateException
- When a merge layout was used and attachToParent was false.public static <T extends ViewDataBinding> T bind(View root)
setDefaultComponent(DataBindingComponent)
.
Prefer using the generated Binding's bind
method to ensure type-safe inflation
when it is known that root
has not yet been bound.
root
- The root View of the inflated binding layout.IllegalArgumentException
- when root is not from an inflated binding layout.getBinding(View)
public static <T extends ViewDataBinding> T bind(View root, DataBindingComponent bindingComponent)
Prefer using the generated Binding's bind
method to ensure type-safe inflation
when it is known that root
has not yet been bound.
root
- The root View of the inflated binding layout.bindingComponent
- The DataBindingComponent to use in data binding.IllegalArgumentException
- when root is not from an inflated binding layout.getBinding(View)
public static <T extends ViewDataBinding> T findBinding(View view)
view
is not a
binding layout root, its parents will be searched for the binding. If there is no binding,
null
will be returned.
This differs from getBinding(View)
in that findBinding takes any view in the
layout and searches for the binding associated with the root. getBinding
takes only the root view.
view
- A View
in the bound layout.null
if
view is not part of a bound layout.public static <T extends ViewDataBinding> T getBinding(View view)
null
will be returned. This uses the DataBindingComponent set in
setDefaultComponent(DataBindingComponent)
.view
- The root View
in the layout with binding.null
if
either the view is not a root View for a layout or view hasn't been bound.public static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId)
activity
- The Activity whose content View should change.layoutId
- The resource ID of the layout to be inflated, bound, and set as the
Activity's content.public static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId, DataBindingComponent bindingComponent)
bindingComponent
- The DataBindingComponent to use in data binding.activity
- The Activity whose content View should change.layoutId
- The resource ID of the layout to be inflated, bound, and set as the
Activity's content.public static String convertBrIdToString(int id)
id
- The integer id, which should be a field from BR class.