【Android Jetpack高手日志】DataBinding 从入门到精通

本文详细介绍了Android DataBinding的初始化、调用流程,包括MainActivity中setUserInfo的执行过程,以及未调用setUser时的数据绑定机制。通过源码分析,揭示了DataBinding如何通过APT生成相关类,并探讨了BR文件的作用。文中还提到了DataBindingUtil的 setContentView 方法和bind方法,以及如何设置和监听数据绑定。最后,强调了DataBinding在UI展示和减少判空处理上的优势,并鼓励开发者深入理解DataBinding的内部原理。
摘要由CSDN通过智能技术生成

/>

</androidx.constraintlayout.widget.ConstraintLayout>

这个文件其实就是移除了<layout>、<data>标签的布局文件,里面的 tag 就是我们上面对应<Target>标签中的tagExpression attribute="android:text" text="userInfo.age" <Expression>中的具体属性对应具体的值。

初始化

再来从val activityBinding: ActivityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)来分析源码,

//DataBindingUtil.java

public static T setContentView(@NonNull Activity activity,

int layoutId, @Nullable DataBindingComponent bindingComponent) {

activity.setContentView(layoutId); //还是需要setContentView

View decorView = activity.getWindow().getDecorView();

ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);

return bindToAddedViews(bindingComponent, contentView, 0, layoutId);

}

我们设置的activity_xxx.xml其实是在android.R.id.content下面的,继续来看bindToAddedViews方法

//DataBindingUtil.java

private static T bindToAddedViews(DataBindingComponent component,

ViewGroup parent, int startChildren, int layoutId) {

final int endChildren = parent.getChildCount();

final int childrenAdded = endChildren - startChildren;

if (childrenAdded == 1) {

final View childView = parent.getChildAt(endChildren - 1);

return bind(component, childView, layoutId); //调用bind

} else {

final View[] children = new View[childrenAdded];

for (int i = 0; i < childrenAdded; i++) {

children[i] = parent.getChildAt(i + startChildren);

}

return bind(component, children, layoutId); //调用bind

}

}

}

最终会调到bind方法

//DataBindingUtil.java

static T bind(DataBindingComponent bindingComponent, View root,

int layoutId) {

return (T) sMapper.getDataBinder(bindingComponent, root, layoutId);

}

sMapperDataBinderMapper,其真正实现类是通过 APT 生成的DataBinderMapperImpl(app/build/generated/ap_generated_sources/debug/out/com/jackie/jetpackdemo/DataBinderMapperImpl.java)

public class DataBinderMapperImpl extends DataBinderMapper {

···

@Override

public ViewDataBinding getDataBinder(DataBindingComponent component, View view, int layoutId) {

int localizedLayoutId = INTERNAL_LAYOUT_ID_LOOKUP.get(layoutId);

if(localizedLayoutId > 0) {

final Object tag = view.getTag();

if(tag == null) {

throw new RuntimeException(“view must have a tag”);

}

switch(localizedLayoutId) {

case LAYOUT_ACTIVITYMAIN: {

if (“layout/activity_main_0”.equals(tag)) {

return new ActivityMainBindingImpl(component, view); //关键代码,new ActivityMainBindingImpl

}

···

接下来我们来分析ActivityMainBindingImpl(app/build/generated/ap_generated_sources/debug/out/com/jackie/jetpackdemo/databinding/ActivityMainBindingImpl.java)这个类,它也是 APT 生成的,

//ActivityMainBindingImpl.java

public ActivityMainBindingImpl(@Nullable androidx.databinding.DataBindingComponent bindingComponent, @NonNull View root) {

this(bindingComponent, root, mapBindings(bi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值