1.预解析
所有DataBinding的xml会先被预处理成xml文件名 n后面加上-layout的xml预处理文件
比如abc.xml被预处理后的名称是abc-layout.xml,同时也会生成新的同名layout文件并给没有id的view会打上tag。
在/app/build/intermediates/data_binding_layout_info_type_merge/release/mergeReleaseResources/out路径下
databinding库会根据@Bindable注解生成BR文件,同时BR文件还会包括xml里的data变量
- BindingAdatpar
每个BindingAdatpar的注解会被预处理生成setter_store 文件,打包到aar里,并且在主项目的该目录下合并:app/build/intermediates/data_binding_dependency_artifacts/release/dataBindingMergeDependencyArtifactsRelease
3.Binding文件创建与初始化
根据-layout.xml创建,同时创建一个DataBinderMapperImpl文件,他继承于MergedDataBinderMapper,并且会根据所有的databinding文件创建一个从layoutid到打bindingimpl类的映射。
@Override
public ViewDataBinding getDataBinder(DataBindingComponent bindingComponent, View view,
int layoutId) {
//从所有的mapper里找到一个有对应映射的databinding对象
for(DataBinderMapper mapper : mMappers) {
ViewDataBinding result = mapper.getDataBinder(bindingComponent, view, layoutId);
if (result != null) {
return result;
}
}
if (loadFeatures()) {
return getDataBinder(bindingComponent, view, layoutId);
}
return null;
}
先根据xml构建viewgroup对象root,之后调用DataBindingUtil方法传入getDefaultComponent跟root,前者负责处理bindingAdapter跟BindingComponent。
之后遍历之前生成的-layout文件给每个控件初始化根据id跟tag找到对应view,之后移出root里的无用tag
绑定是在setvm的时候创建的:
public void setVm(@Nullable fm.qingting.framework.navi.viewmodel.ToolbarViewModel Vm) {
updateRegistration(0, Vm);
this.mVm = Vm;
synchronized(this) {
mDirtyFlags |= 0x1L;
}
notifyPropertyChanged(BR.vm);
super.requestRebind();
}
protected void registerTo(int localFieldId, Object observable,
CreateWeakListener listenerCreator) {
if (observable == null) {
return;
}
// 创建对象监听并存到mLocalFieldObservers中
WeakListener listener = mLocalFieldObservers[localFieldId];
if (listener == null) {
//这里会使用WeakPropertyListener去创建listener
// CREATE_PROPERTY_LISTENER -> create(...)
listener = listenerCreator.create(this, localFieldId);
mLocalFieldObservers[localFieldId] = listener;
}
// 将监听绑定到Observable对象上
listener.setTarget(observable);
}
private static class WeakPropertyListener extends Observable.OnPropertyChangedCallback
implements ObservableReference<Observable> {
final WeakListener<Observable> mListener;
public WeakPropertyListener(ViewDataBinding binder, int localFieldId) {
mListener = new WeakListener<Observable>(binder, localFieldId, this);
}
@Override
public WeakListener<Observable> getListener() {
return mListener;
}
@Override
public void addListener(Observable target) {
//这里会给Observable添加回调,所以当Observable里调用对应的nofity时会触发这个
target.addOnPropertyChangedCallback(this);
}
@Override
public void removeListener(Observable target) {
target.removeOnPropertyChangedCallback(this);
}
@Override
public void setLifecycleOwner(LifecycleOwner lifecycleOwner) {
}
@Override
public void onPropertyChanged(Observable sender, int propertyId) {
ViewDataBinding binder = mListener.getBinder();
if (binder == null) {
return;
}
Observable obj = mListener.getTarget();
if (obj != sender) {
return; // notification from the wrong object?
}
//这里会触发binding对象的handleFieldChange方法从而触发onFieldChange方法
binder.handleFieldChange(mListener.mLocalFieldId, sender, propertyId);
}
}
@Override
protected boolean onFieldChange(int localFieldId, Object object, int fieldId) {
switch (localFieldId) {
case 0 :
return onChangeVm((fm.qingting.framework.navi.viewmodel.ToolbarViewModel) object, fieldId);
case 1 :
return onChangeNavi((fm.qingting.framework.databinding.NavigationViewBinding) object, fieldId);
}
return false;
}
private void handleFieldChange(int mLocalFieldId, Object object, int fieldId) {
if (mInLiveDataRegisterObserver) {
// We're in LiveData registration, which always results in a field change
// that we can ignore. The value will be read immediately after anyway, so
// there is no need to be dirty.
return;
}
boolean result = onFieldChange(mLocalFieldId, object, fieldId);
if (result) {
//如果onFieldChange返回true则更新
requestRebind();
}
}
private boolean onChangeVm(fm.qingting.framework.navi.viewmodel.ToolbarViewModel Vm, int fieldId) {
//如果vm被更新则刷新所有的property
if (fieldId == BR._all) {
synchronized(this) {
mDirtyFlags |= 0x1L;
}
return true;
}
return false;
}
双向绑定只需要在生成binding文件的时候给view加上对应的监听然后去对应的改变viewmodel的对应值就行,这里不展开了
以上是对databinding实现的一些简单分析,关于lifecycle的相关内容后续再补充