Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "DataBindingComponent" class in android databinding?

Tags:

I have seen the DataBindingComponent in the official API doc.

https://developer.android.com/reference/android/databinding/DataBindingUtil.html

This interface is generated during compilation to contain getters for all used instance BindingAdapters. When a BindingAdapter is an instance method, an instance of the class implementing the method must be instantiated. This interface will be generated with a getter for each class with the name get* where * is simple class name of the declaring BindingAdapter class/interface. Name collisions will be resolved by adding a numeric suffix to the getter.

An instance of this class may also be passed into static or instance BindingAdapters as the first parameter.

If using Dagger 2, the developer should extend this interface and annotate the extended interface as a Component.

However, I cannot find any example usage of this class in the web. Can anyone know what it is and how to use it.

I tried to make some simple code and debug it to see what's is it but it showed null variable on the a variable.

protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);     android.databinding.DataBindingComponent a = DataBindingUtil.getDefaultComponent();     setContentView(binding.getRoot()); } 
like image 967
Long Ranger Avatar asked Sep 02 '16 03:09

Long Ranger


People also ask

What is Databindingutil in Android?

Returns the binding for the given layout root or creates a binding if one does not exist. Prefer using the generated Binding's bind method to ensure type-safe inflation when it is known that root has not yet been bound. View : The root View of the inflated binding layout.

Is Android databinding deprecated?

Recently Android has announced that with Kotlin 1.4. 20, their Android Kotlin Extensions Gradle plugin will be deprecated and will no longer be shipped in the future Kotlin releases. Android Kotlin Extensions plugin brought with it two very cool features : Synthetics let you replace calls to findViewById with kotlinx.

When should I use databinding?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.

What is data binding and its types Android?

In Android, the Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.


1 Answers

After reading the answer from @tynn , the DataBindingComponent class can also be as "object" scope of data binding method. Instead of setting all the method in static way, the following example can be used to be extendable and custom. For example, we setup 3 binding methods for ImageView, TextView and View type. You can setup the interface first ( like Retrofit 2 setup interface for API)

1. Setup 3 interface first

ImageViewBindingInterface.java

public interface ImageViewBindingInterface {     @BindingAdapter({"bind:imageUrl", "bind:error"})     public  void loadImage(ImageView view, String url, Drawable error); } 

TextViewBindingInterface.java

public interface TextViewBindingInterface {     @BindingAdapter({"bind:font"})       void setFont(TextView textView, String fontName); } 

ViewBindingInterface.java

public interface ViewBindingInterface {     @BindingAdapter("android:paddingLeft")     public  void setPaddingLeft(View view, int padding);     @BindingAdapter("android:onViewAttachedToWindow")     public  void setListener(View view, ViewBindingAdapter.OnViewAttachedToWindow attached); } 

2. The DataBindingComponent.java should be updated automatically as @tynn mentioned like the following.

If you have a look at the default DataBindingComponent class in Android Studio you find it located in build/generated/source/apt/dev.

public interface DataBindingComponent {     example.com.testerapplication.binding.ViewBindingInterface getViewBindingInterface();     example.com.testerapplication.binding.TextViewBindingInterface getTextViewBindingInterface();     example.com.testerapplication.binding.ImageViewBindingInterface getImageViewBindingInterface(); } 

3. Build your own implementation method for binding.

BaseImageViewBinding.java

public class BaseImageViewBinding implements ImageViewBindingInterface{     @Override     public void loadImage(ImageView view, String url, Drawable error) {           Picasso.with(view.getContext()).load(url).error(error).into(view);     } } 

BaseTextViewBinding.java

public class BaseTextViewBinding implements TextViewBindingInterface {     @Override     public void setFont(TextView textView, String fontName) {         textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));     } } 

BaseViewBinding.java

public class BaseViewBinding implements ViewBindingInterface {     @Override     public void setPaddingLeft(View view, int padding) {         view.setPadding(padding,                 view.getPaddingTop(),                 view.getPaddingRight(),                 view.getPaddingBottom());     }     @Override     public void setListener(View view, ViewBindingAdapter.OnViewAttachedToWindow attached) {      } } 

4. Set your OwnDatabindingComponent

public class MyOwnDefaultDataBindingComponent implements android.databinding.DataBindingComponent {     @Override     public ViewBindingInterface getViewBindingInterface() {         return new BaseViewBinding();     }     @Override     public TextViewBindingInterface getTextViewBindingInterface() {         return new BaseTextViewBinding();     }     @Override     public ImageViewBindingInterface getImageViewBindingInterface() {         return new BaseImageViewBinding();     } } 

5. Setup your default DataBindingComponent in the Application

public class MyApplication extends Application {     public void onCreate() {         super.onCreate();         DataBindingUtil.setDefaultComponent(new MyOwnDefaultDataBindingComponent());     } } 

Using this method should be fine to make the custom data binding in a custom way and can be extensible.

like image 151
Long Ranger Avatar answered Oct 21 '22 14:10

Long Ranger