写在前面:Jetpack的更新速度非常快,可能你一个月前看WorkManager是这样用的,下个月这个使用方法就有可能被废弃了(我看源码的时候是遇到过的,而且源码也变了,但核心原理是不变的),所以我们这一系列文章偏重讲原理,使用就一带而过(因为讲了也没用啊,会变的。。。。。,读者使用最好看官方文档官方文档
,当然我这里讲的也是截止到目前的最新用法)。
Jetpack使用(一)Lifecycles核心原理
Jetpack使用(二)LiveData核心原理
Jetpack使用(三)DataBinding核心原理
Jetpack使用(四)ViewModel核心原理
Jetpack使用(五)Navigation核心原理
Jetpack使用(六) WorkManager的4种用法
DataBinding
是 Google 在 Jetpack 中推出的一款数据绑定的支持库,利用该库可以实现在页面组件和数据的双向绑定,类似与MVVM。
具体使用
- 在build.gradle里配置
dataBinding{
enabled true
}
- 新建一个实体类继承BaseObservable,并且在对应的get方法上加上注解 @Bindable,在set方法里加 notifyPropertyChanged(BR.name);
public class User extends BaseObservable {
@Bindable
private String name;
@Bindable
private String password;
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- 在布局文件里导入要用的实体类
- MainActivity里具体去使用
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
user = new User("小三爷", "123");
user.setName(user.getName() + "1");
binding.setVariable(BR.user, user);
}
}
在你想要更新的地方,调用binding.setVariable(BR.user, user)就ok。
核心原理分析
当我们的代码在编译的时候,系统会给我们的xml文件分离成两个文件
1、app/build/imtermediates/data_binding_layout_info_type_merge/
debug/activity_main-layout.xml,相当于把我们的xml文件用另一种xml方式翻译了一下
false
false
2、app/build/imtermediates/incremental/mergeDebugResources/stripped.dir/
layout/activity_main.xml,把我们的xml去掉了包含的代码块
还有会通过apt生成三个类文件ActivityMainBindingImpl、BR、DataBinderMapperImpl
BR就相当于R文件,里面存放xml布局里控件的id
public class BR {
public static final int _all = 0;
public static final int password = 1;
public static final int name = 2;
public static final int user = 3;
}
知道了这些我们现在真正进入源码分析阶段
流程一、binding.setVariable(设置数据)
我们在在MainActicty里调用setVariable来设置数据的时候,会进入到ViewDataBinding类
public abstract boolean setVariable(int variableId, @Nullable Object value);
这是一个抽象类,具体实现会在ActivityMainBingdingImpl里,
@Override
public boolean setVariable(int variableId, @Nullable Object variable) {
boolean variableSet = true;
if (BR.user == variableId) {
setUser((com.example.databinding_demo.User) variable);
}
else {
variableSet = false;
}
return variableSet;
}
setVariable里会调用到ActivityMainBingdingImpl里的另一个方法setUser,我们再点进去
public void setUser(@Nullable com.example.databinding_demo.User User) {
updateRegistration(0, User);
this.mUser = User;
synchronized(this) {
mDirtyFlags |= 0x1L;
}
notifyPropertyChanged(BR.user);
super.requestRebind();
}
setUser里先调用注册updateRegistration方法,然后把User设置给成员变量mUser
初始化了mDirtyFlags标志,最后是调用notifyPropertyChanged通知更新。
我们先进入注册updateRegistration方法
protected boolean updateRegistration(int localFieldId, Observable observable) {
return updateRegistration(localFieldId, observable, CREATE_PROPERTY_LISTENER);
}
CREATE_PROPERTY_LISTENER实际是返回了WeakPropertyListener对象
private boolean updateRegistration(int localFieldId, Object observable,
CreateWeakListener listenerCreator) {
if (observable == null) {
return unregisterFrom(localFieldId);
}
WeakListener listener = mLocalFieldObservers[localFieldId];
if (listener == null) {
registerTo(localFieldId, observable, listenerCreator);
return true;
}
if (listener.getTarget() == observable) {
return false;//nothing to do, same object
}
unregisterFrom(localFieldId);
registerTo(localFieldId, observable, listenerCreator);
return true;
}
最后会走到这个方法里来,我们再点到registerTo方法
protected void registerTo(int localFieldId, Object observable,
CreateWeakListener listenerCreator) {
if (observable == null) {
return;
}
WeakListener listener = mLocalFieldObservers[localFieldId];
if (listener == null) {
listener = listenerCreator.create(this, localFieldId);
mLocalFieldObservers[localFieldId] = listener;
if (mLifecycleOwner != null) {
listener.setLifecycleOwner(mLifecycleOwner);
}
}
listener.setTarget(observable);
}
这个方法实会判断mLocalFieldObservers里是否存在WeakListener listener,如果不存在就创建一个存进去,mLocalFieldObservers里面保存的就是我们控件在BR文件里的id,每个id就对应一个监听器,然后通过listener.setLifecycleOwner(mLifecycleOwner);把lifecycle关联起来,再通过listener.setTarget(observable);把User对象关联起来,所以这个注册方法简单的说就是:把我们的实体对象User(观察者)、DataBinding、Lifecycle(被观察者这里就是activity)建立绑定关系,还有把控件的BR文件里的id放到一个数组里。
流程二、setContentView(读取XML信息)
我们在MainActivty里调用DataBindingUtil.setContentView(this,R.layout.activity_main)实际会调用到
DataBinderMapperImpl的getDataBinder方法,
@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);
}
throw new IllegalArgumentException("The tag for activity_main is invalid. Received: " + tag);
}
}
}
return null;
}
ActivityMainBindingImpl再点进去,最后会调到ViewDataBinding的mapBindings的方法
private static void mapBindings(DataBindingComponent bindingComponent, View view,
Object[] bindings, IncludedLayouts includes, SparseIntArray viewsWithIds,
boolean isRoot) {
final int indexInIncludes;
final ViewDataBinding existingBinding = getBinding(view);
if (existingBinding != null) {
return;
}
Object objTag = view.getTag();
final String tag = (objTag instanceof String) ? (String) objTag : null;
boolean isBound = false;
if (isRoot && tag != null && tag.startsWith("layout")) {
final int underscoreIndex = tag.lastIndexOf('_');
if (underscoreIndex > 0 && isNumeric(tag, underscoreIndex + 1)) {
final int index = parseTagInt(tag, underscoreIndex + 1);
if (bindings[index] == null) {
bindings[index] = view;
}
indexInIncludes = includes == null ? -1 : index;
isBound = true;
} else {
indexInIncludes = -1;
}
} else if (tag != null && tag.startsWith(BINDING_TAG_PREFIX)) {
int tagIndex = parseTagInt(tag, BINDING_NUMBER_START);
if (bindings[tagIndex] == null) {
bindings[tagIndex] = view;
}
isBound = true;
indexInIncludes = includes == null ? -1 : tagIndex;
} else {
// Not a bound view
indexInIncludes = -1;
}
if (!isBound) {
final int id = view.getId();
if (id > 0) {
int index;
if (viewsWithIds != null && (index = viewsWithIds.get(id, -1)) >= 0 &&
bindings[index] == null) {
bindings[index] = view;
}
}
}
if (view instanceof ViewGroup) {
final ViewGroup viewGroup = (ViewGroup) view;
final int count = viewGroup.getChildCount();
int minInclude = 0;
for (int i = 0; i < count; i++) {
final View child = viewGroup.getChildAt(i);
boolean isInclude = false;
if (indexInIncludes >= 0 && child.getTag() instanceof String) {
String childTag = (String) child.getTag();
if (childTag.endsWith("_0") &&
childTag.startsWith("layout") && childTag.indexOf('/') > 0) {
// This *could* be an include. Test against the expected includes.
int includeIndex = findIncludeIndex(childTag, minInclude,
includes, indexInIncludes);
if (includeIndex >= 0) {
isInclude = true;
minInclude = includeIndex + 1;
final int index = includes.indexes[indexInIncludes][includeIndex];
final int layoutId = includes.layoutIds[indexInIncludes][includeIndex];
int lastMatchingIndex = findLastMatching(viewGroup, i);
if (lastMatchingIndex == i) {
bindings[index] = DataBindingUtil.bind(bindingComponent, child,
layoutId);
} else {
final int includeCount = lastMatchingIndex - i + 1;
final View[] included = new View[includeCount];
for (int j = 0; j < includeCount; j++) {
included[j] = viewGroup.getChildAt(i + j);
}
bindings[index] = DataBindingUtil.bind(bindingComponent, included,
layoutId);
i += includeCount - 1;
}
}
}
}
if (!isInclude) {
mapBindings(bindingComponent, child, bindings, includes, viewsWithIds, false);
}
}
}
}
mapBindings里就是解析xml文件信息,并存入bingdings数组,
流程三、notifyPropertyChanged(更新数据)
我们知道在流程一setVariable方法之后会调用notifyPropertyChanged去更新数据,我们点进去最终会跑到ViewDataBingding的 onPropertyChanged里
@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?
}
binder.handleFieldChange(mListener.mLocalFieldId, sender, propertyId);
}
一层层点进去发现是一个抽象方法
protected abstract boolean onFieldChange(int localFieldId, Object object, int fieldId);
最终它的实现类在ActivityMainBingdingImpl的onFieldChange里
@Override
protected boolean onFieldChange(int localFieldId, Object object, int fieldId) {
switch (localFieldId) {
case 0 :
return onChangeUser((com.example.databinding_demo_20200329.User) object, fieldId);
}
return false;
}
private boolean onChangeUser(com.example.databinding_demo_20200329.User User, int fieldId) {
if (fieldId == BR._all) {
synchronized(this) {
mDirtyFlags |= 0x1L;
}
return true;
}
else if (fieldId == BR.name) {
synchronized(this) {
mDirtyFlags |= 0x2L;
}
return true;
}
else if (fieldId == BR.password) {
synchronized(this) {
mDirtyFlags |= 0x4L;
}
return true;
}
return false;
}
这个方法去通过位或操作设置mDirtyFlags的初始值的,我们再看到ViewDataBinding里的一个静态代码块
static {
if (VERSION.SDK_INT < VERSION_CODES.KITKAT) {
ROOT_REATTACHED_LISTENER = null;
} else {
ROOT_REATTACHED_LISTENER = new OnAttachStateChangeListener() {
@TargetApi(VERSION_CODES.KITKAT)
@Override
public void onViewAttachedToWindow(View v) {
// execute the pending bindings.
final ViewDataBinding binding = getBinding(v);
binding.mRebindRunnable.run();
v.removeOnAttachStateChangeListener(this);
}
@Override
public void onViewDetachedFromWindow(View v) {
}
};
}
}
这里有个监听器,在监听executeBindings,这个方法最终的实现类在ActivityMainBindingImpl里
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
java.lang.String userName = null;
com.example.databinding_demo_20200329.User user = mUser;
java.lang.String userPassword = null;
if ((dirtyFlags & 0xfL) != 0) {
if ((dirtyFlags & 0xbL) != 0) {
if (user != null) {
// read user.name
userName = user.getName();
}
}
if ((dirtyFlags & 0xdL) != 0) {
if (user != null) {
// read user.password
userPassword = user.getPassword();
}
}
}
// batch finished
if ((dirtyFlags & 0xbL) != 0) {
// api target 1
androidx.databinding.adapters.TextViewBindingAdapter.setText(this.tv1, userName);
}
if ((dirtyFlags & 0xdL) != 0) {
// api target 1
androidx.databinding.adapters.TextViewBindingAdapter.setText(this.tv2, userPassword);
}
}
我们前面通过onFieldChange设置的标志就是在这里通过位与操作来进行控件的更新的。到此,DataBingding核心原理就一目了然了