什么是"DataBindingComponent"? android数据绑定中的类? [英] What is "DataBindingComponent" class in android databinding?

查看:226
本文介绍了什么是"DataBindingComponent"? android数据绑定中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在官方API文档中看到了DataBindingComponent.

I have seen the DataBindingComponent in the official API doc.

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

此接口是在编译期间生成的,其中包含用于 所有使用的实例BindingAdapters.当BindingAdapter是 实例方法,实现该方法的类的实例必须 被实例化.该接口将通过getter生成,用于 每个名称为get *的类,其中*是简单的类名 声明BindingAdapter类/接口.名称冲突将是 通过在吸气剂上添加数字后缀来解决.

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.

此类的实例也可以传递给static或instance BindingAdapters作为第一个参数.

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

如果使用Dagger 2,则开发人员应扩展此接口并 将扩展接口注释为组件.

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.

我试图制作一些简单的代码并对其进行调试,以查看它是什么,但它在a变量上显示了null变量.

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());
}

推荐答案

来自文档我们知道

此接口在编译期间生成,以包含所有使用的实例BindingAdapter的吸气剂.当BindingAdapter是实例方法时,必须实例化实现该方法的类的实例.将为每个具有名称为get *的类的getter生成此接口,其中*是声明BindingAdapter类/接口的简单类名.名称冲突将通过在getter上添加数字后缀来解决.

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.

此类的实例也可以作为第一个参数传递到静态或实例BindingAdapters中.

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

如果使用Dagger 2,则开发人员应扩展此接口,并将扩展的接口注释为组件.

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

这告诉我们此接口用于为实现自定义@BindingAdapter方法的实例注入工厂.这样,您可以为不同的情况或布局配置数据绑定,或为其提供更一般的状态.如果您查看Android Studio中的默认DataBindingComponent类,则会发现它位于 build/generated/source/apt/dev 中.

This tells us this interface is used and generated for injecting a factory for instances implementing custom @BindingAdapter methods. Like this you can configure the data bindings for different situations or layouts or supply it with a more general state. If you have a look at the default DataBindingComponent class in Android Studio you find it located in build/generated/source/apt/dev.

DataBindingComponent可以使用的方法是

  • DataBindingUtil.setDefaultComponent()
  • DataBindingUtil.inflate()
  • DataBindingUtil.bind()

考虑您定义类似的接口

public interface Foo {
    @BindingAdapter("foobar")
    void fooBar(View view, String baz);
}

生成android.databinding.DataBindingComponent接口

public interface DataBindingComponent {
    di.pkg.Foo getFoo();
}

@BindingAdapter主机现在已在您的数据绑定中使用,但是您需要自己实现该接口,并将其与上述给定的方法之一一起使用

This @BindingAdapter host now gets used in your data bindings, but you need to implement the interface yourself and use it with one of the methods given above like

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()中获得了空值,因为您从未自己设置默认组件.

In your example you get null from DataBindingUtil.getDefaultComponent() because you've never set the default component yourself.

这篇关于什么是"DataBindingComponent"? android数据绑定中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆