/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.databinding; import android.app.Activity; import android.support.annotation.Nullable; import android.view.InflateException; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; /** * Utility class to create {@link ViewDataBinding} from layouts. */ public class DataBindingUtil { private static DataBinderMapper sMapper = new DataBinderMapper(); private static DataBindingComponent sDefaultComponent = null; /** * Prevent DataBindingUtil from being instantiated. */ private DataBindingUtil() {} /** * Set the default {@link 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 void setDefaultComponent(DataBindingComponent bindingComponent) {
sDefaultComponent = bindingComponent;
}
/**
* Returns the default {@link DataBindingComponent} used in data binding. This can be
* null
if no default was set in
* {@link #setDefaultComponent(DataBindingComponent)}.
*
* @return the default {@link DataBindingComponent} used in data binding. This can be
* null
if no default was set in
* {@link #setDefaultComponent(DataBindingComponent)}.
*/
public static DataBindingComponent getDefaultComponent() {
return sDefaultComponent;
}
/**
* Inflates a binding layout and returns the newly-created binding for that layout.
* This uses the DataBindingComponent set in
* {@link #setDefaultComponent(DataBindingComponent)}.
*
* Use this version only if
* Use this version only if
* Prefer using the generated Binding's
* Prefer using the generated Binding's
* This differs from {@link #getBinding(View)} in that findBinding takes any view in the
* layout and searches for the binding associated with the root. layoutId
is unknown in advance. Otherwise, use
* the generated Binding's inflate method to ensure type-safe inflation.
*
* @param inflater The LayoutInflater used to inflate the binding layout.
* @param layoutId The layout resource ID of the layout to inflate.
* @param 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.)
* @param 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.
* @return The newly-created binding for the inflated layout or null
if
* the layoutId wasn't for a binding layout.
* @throws InflateException When a merge layout was used and attachToParent was false.
* @see #setDefaultComponent(DataBindingComponent)
*/
public static layoutId
is unknown in advance. Otherwise, use
* the generated Binding's inflate method to ensure type-safe inflation.
*
* @param inflater The LayoutInflater used to inflate the binding layout.
* @param layoutId The layout resource ID of the layout to inflate.
* @param 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.)
* @param 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.
* @param bindingComponent The DataBindingComponent to use in the binding.
* @return The newly-created binding for the inflated layout or null
if
* the layoutId wasn't for a binding layout.
* @throws InflateException When a merge layout was used and attachToParent was false.
*/
public static bind
method to ensure type-safe inflation
* when it is known that root
has not yet been bound.
*
* @param root The root View of the inflated binding layout.
* @return A ViewDataBinding for the given root View. If one already exists, the
* existing one will be returned.
* @throws IllegalArgumentException when root is not from an inflated binding layout.
* @see #getBinding(View)
*/
@SuppressWarnings("unchecked")
public static bind
method to ensure type-safe inflation
* when it is known that root
has not yet been bound.
*
* @param root The root View of the inflated binding layout.
* @param bindingComponent The DataBindingComponent to use in data binding.
* @return A ViewDataBinding for the given root View. If one already exists, the
* existing one will be returned.
* @throws IllegalArgumentException when root is not from an inflated binding layout.
* @see #getBinding(View)
*/
@SuppressWarnings("unchecked")
public static view
is not a
* binding layout root, its parents will be searched for the binding. If there is no binding,
* null
will be returned.
* getBinding
* takes only the root view.
*
* @param view A View
in the bound layout.
* @return The ViewDataBinding associated with the given view or null
if
* view is not part of a bound layout.
*/
public static null
will be returned. This uses the DataBindingComponent set in
* {@link #setDefaultComponent(DataBindingComponent)}.
*
* @param view The root View
in the layout with binding.
* @return The ViewDataBinding associated with the given view or null
if
* either the view is not a root View for a layout or view hasn't been bound.
*/
public static