android 数据绑定中的 "DataBindingComponent" class 是什么?
What is "DataBindingComponent" class in android databinding?
我在官方 API 文档中看到了 DataBindingComponent。
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.
但是,我无法在网络上找到此 class 的任何示例用法。谁能知道它是什么以及如何使用它。
我试图编写一些简单的代码并调试它以查看它是什么,但它在 a
变量上显示空变量。
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());
}
从documentation我们知道
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.
这告诉我们这个接口是用来为实现自定义 @BindingAdapter
方法的实例注入工厂而生成的。像这样,您可以为不同的情况或布局配置数据绑定,或者为其提供更通用的状态。如果您查看 Android Studio 中的默认值 DataBindingComponent
class,您会发现它位于 build/generated/source/apt/dev .
您可以使用 DataBindingComponent
的方法是
考虑到你定义了一个像
这样的接口
public interface Foo {
@BindingAdapter("foobar")
void fooBar(View view, String baz);
}
生成了一个android.databinding.DataBindingComponent
界面
public interface DataBindingComponent {
di.pkg.Foo getFoo();
}
这个 @BindingAdapter
主机现在可以在您的数据绑定中使用,但您需要自己实现该接口并将其与上面给出的方法之一一起使用,例如
DataBindingUtil.setDefaultComponent(new DataBindingComponent(){
@Override
public Foo getFoo() {
return new Foo() {
@Override
public void fooBar(View view, String baz) {
if (view instanceof TextView) ((TextView) view).setText(baz);
}
};
}
});
在您的示例中,您从 DataBindingUtil.getDefaultComponent()
中得到 null,因为您从未自己设置过默认组件。
看完@tynn 的回答后,DataBindingComponent class 也可以作为数据绑定方法的"object" 作用域。以下示例可用于扩展和自定义,而不是以静态方式设置所有方法。例如,我们为 ImageView、TextView 和 View 类型设置了 3 种绑定方法。您可以先设置界面(如API的Retrofit 2设置界面)
1.先设置3界面
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。 DataBindingComponent.java 应该像下面提到的 @tynn 一样自动更新。
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。构建您自己的绑定实现方法。
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。设置您的 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.在 Application
中设置默认的 DataBindingComponent
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
DataBindingUtil.setDefaultComponent(new MyOwnDefaultDataBindingComponent());
}
}
使用此方法应该可以以自定义方式进行自定义数据绑定并且可以扩展。
我在官方 API 文档中看到了 DataBindingComponent。
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.
但是,我无法在网络上找到此 class 的任何示例用法。谁能知道它是什么以及如何使用它。
我试图编写一些简单的代码并调试它以查看它是什么,但它在 a
变量上显示空变量。
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());
}
从documentation我们知道
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.
这告诉我们这个接口是用来为实现自定义 @BindingAdapter
方法的实例注入工厂而生成的。像这样,您可以为不同的情况或布局配置数据绑定,或者为其提供更通用的状态。如果您查看 Android Studio 中的默认值 DataBindingComponent
class,您会发现它位于 build/generated/source/apt/dev .
您可以使用 DataBindingComponent
的方法是
考虑到你定义了一个像
这样的接口public interface Foo {
@BindingAdapter("foobar")
void fooBar(View view, String baz);
}
生成了一个android.databinding.DataBindingComponent
界面
public interface DataBindingComponent {
di.pkg.Foo getFoo();
}
这个 @BindingAdapter
主机现在可以在您的数据绑定中使用,但您需要自己实现该接口并将其与上面给出的方法之一一起使用,例如
DataBindingUtil.setDefaultComponent(new DataBindingComponent(){
@Override
public Foo getFoo() {
return new Foo() {
@Override
public void fooBar(View view, String baz) {
if (view instanceof TextView) ((TextView) view).setText(baz);
}
};
}
});
在您的示例中,您从 DataBindingUtil.getDefaultComponent()
中得到 null,因为您从未自己设置过默认组件。
看完@tynn 的回答后,DataBindingComponent class 也可以作为数据绑定方法的"object" 作用域。以下示例可用于扩展和自定义,而不是以静态方式设置所有方法。例如,我们为 ImageView、TextView 和 View 类型设置了 3 种绑定方法。您可以先设置界面(如API的Retrofit 2设置界面)
1.先设置3界面
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。 DataBindingComponent.java 应该像下面提到的 @tynn 一样自动更新。
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。构建您自己的绑定实现方法。
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。设置您的 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.在 Application
中设置默认的 DataBindingComponentpublic class MyApplication extends Application {
public void onCreate() {
super.onCreate();
DataBindingUtil.setDefaultComponent(new MyOwnDefaultDataBindingComponent());
}
}
使用此方法应该可以以自定义方式进行自定义数据绑定并且可以扩展。