gpt4 book ai didi

android - android数据绑定(bind)中的 "DataBindingComponent"类是什么?

转载 作者:IT老高 更新时间:2023-10-28 23:18:44 39 4
gpt4 key购买 nike

我在官方 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.

但是,我在网络上找不到此类的任何示例用法。谁能知道它是什么以及如何使用它。

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

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

最佳答案

阅读@tynn 的回答后,DataBindingComponent 类也可以作为数据绑定(bind)方法的“对象”范围。不是以静态方式设置所有方法,而是可以使用以下示例进行扩展和自定义。例如,我们为 ImageView、TextView 和 View 类型设置了 3 种绑定(bind)方法。您可以先设置接口(interface)(如 API 的 Retrofit 2 设置接口(interface))

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.构建自己的绑定(bind)实现方法。

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.在应用程序中设置默认的 DataBindingComponent

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

使用此方法应该可以以自定义方式进行自定义数据绑定(bind),并且可以扩展。

关于android - android数据绑定(bind)中的 "DataBindingComponent"类是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39283855/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com