Android-DataBinding原理分析

一、分析案例

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data><!--这里面就用来定义数据源-->
        <variable
            name="user"
            type= "com.example.databindingdemo.User"/>
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:textSize="50sp"
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:textSize="50sp"
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.pwd}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>
</layout>
public class User extends BaseObservable {
    private String name;
    private String pwd;

    public User(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
        notifyPropertyChanged(BR.pwd);
    }
}

在MainActivity中,调用下面的方法:

binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
user=new User("zzq","778899");
binding.setUser(user);

二、DataBinding源码分析

1.首先,看使用了DataBinding的布局变化:

app\build\intermediates\data_binding_layout_info_type_merge\debug\out

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Layout
    directory="layout"
    filePath="app\src\main\res\layout\activity_main.xml"
    isBindingData="true"
    isMerge="false"
    layout="activity_main"
    modulePackage="com.example.databindingdemo_20210117"
    rootNodeType="android.widget.LinearLayout">

    <Variables
        name="user"
        declared="true"
        type="com.example.databindingdemo_20210117.User">

        <location
            endLine="7"
            endOffset="62"
            startLine="5"
            startOffset="8" />
    </Variables>

    <Targets>

        <Target
            tag="layout/activity_main_0"
            view="LinearLayout">

            <Expressions />

            <location
                endLine="35"
                endOffset="18"
                startLine="9"
                startOffset="4" />
        </Target>

        <Target
            id="@+id/tv1"
            tag="binding_1"
            view="TextView">

            <Expressions>

                <Expression
                    attribute="android:text"
                    text="user.name">

                    <Location
                        endLine="19"
                        endOffset="38"
                        startLine="19"
                        startOffset="12" />

                    <TwoWay>false</TwoWay>

                    <ValueLocation
                        endLine="19"
                        endOffset="36"
                        startLine="19"
                        startOffset="28" />
                </Expression>
            </Expressions>

            <location
                endLine="23"
                endOffset="55"
                startLine="14"
                startOffset="8" />
        </Target>

        <Target
            id="@+id/tv2"
            tag="binding_2"
            view="TextView">

            <Expressions>

                <Expression
                    attribute="android:text"
                    text="user.pwd">

                    <Location
                        endLine="30"
                        endOffset="37"
                        startLine="30"
                        startOffset="12" />

                    <TwoWay>false</TwoWay>

                    <ValueLocation
                        endLine="30"
                        endOffset="35"
                        startLine="30"
                        startOffset="28" />
                </Expression>
            </Expressions>

            <location
                endLine="34"
                endOffset="55"
                startLine="25"
                startOffset="8" />
        </Target>
    </Targets>
</Layout>

可以看到,这里定义了多个Target标签,这些Target的定义,其实就是定义对应的tag,将tag与activity_main.xml布局中的对应的View的id对应起来
经过DataBinding变化后的布局,会多出tag。
app/build/imtermediates/incremental/mergeDebugResources/stripped.dir/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

                                                       
                                                   
    
                 
                       
                                                               
           
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" android:tag="layout/activity_main_0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools">

        <TextView
            android:textSize="50sp"
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="binding_1"    
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:textSize="50sp"
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tag="binding_2"   
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>

2.DataBindingUtil.setContentView原理

binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
(1)DataBindingUtil.setContentView()

其实DataBindingUtil的setContentView()方法,主要就是调用activity的setContentView设置布局,并且绑定添加对应的View

public static <T extends ViewDataBinding> T setContentView(@NonNull Activity activity,
        int layoutId) {
    return setContentView(activity, layoutId, sDefaultComponent);
}
public static <T extends ViewDataBinding> T setContentView(@NonNull Activity activity,
        int layoutId, @Nullable DataBindingComponent bindingComponent) {
    // 调用Activity的setContentView设置布局
    activity.setContentView(layoutId);
    View decorView = activity.getWindow().getDecorView();
    // 获取DecorView中的content
    ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
    return bindToAddedViews(bindingComponent, contentView, 0, layoutId);
}
(2)DataBindingUtil.bindToAddedViews()
private static <T extends ViewDataBinding> T bindToAddedViews(DataBindingComponent component,
        ViewGroup parent, int startChildren, int layoutId) {
    final int endChildren = parent.getChildCount();
    final int childrenAdded = endChildren - startChildren;
    // 如果childrenAdded==1,则就只有一个子View
    // 如果不等于1,则有多个
    if (childrenAdded == 1) {
        final View childView = parent.getChildAt(endChildren - 1);
        return bind(component, childView, layoutId);
    } 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);
    }
}
(3)DataBindingUtil.bind()
static <T extends ViewDataBinding> T bind(DataBindingComponent bindingComponent, View[] roots,
        int layoutId) {
    return (T) sMapper.getDataBinder(bindingComponent, roots, layoutId);
}

static <T extends ViewDataBinding> T bind(DataBindingComponent bindingComponent, View root,
        int layoutId) {
    return (T) sMapper.getDataBinder(bindingComponent, root, layoutId);
}

这里的sMapper是一个DataBinderMapper对象,其实现类是DataBinderMapperImpl
DataBinderMapperImpl是通过apt注解处理器生成的。
这里的sMapper.getDataBinder()其实就是调用的MergedDataBinderMapper的getDataBinder()方法
而sMapper中的数据,其实就是DataBinderMapperImpl的构造器中调用其父类MergedDataBinderMapper 的addMapper()方法添加的对象

public class DataBinderMapperImpl extends MergedDataBinderMapper {
  DataBinderMapperImpl() {
    addMapper(new com.example.databindingdemo.DataBinderMapperImpl());
  }
}

在DataBinding中有两个DataBinderMapperImpl类,一个是上面这个在androidx.databinding包下,继承了MergedDataBinderMapper的,另一个是在com.example.databindingdemo应用包下,直接继承DataBinderMapper。其实MergedDataBinderMapper也是继承自DataBinderMapper

(4)MergedDataBinderMapper.getDataBinder()
@Override
public ViewDataBinding getDataBinder(DataBindingComponent bindingComponent, View view,
        int layoutId) {
    // mMappers集合中的数据就是来源于androidx.databinding.DataBinderMapperImpl
    // 的构造器中调用addMapper方法传入的对象添加的
    // 所以这里的mapper就是com.example.databindingdemo.DataBinderMapperImpl对象
    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;
}
(5)com.example.databindingdemo.DataBinderMapperImpl.getDataBinder

这里要注意两点,就是如果是布局的顶层View,比如tag为layout/activity_main_0,那么就会new一个ActivityMainBindingImpl对象。这个tag,其实可以从前面看到的app/build/imtermediates/incremental/mergeDebugResources/stripped.dir/layout/activity_main.xml布局中的LinearLayout的tag知道

@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;
}

@Override
public ViewDataBinding getDataBinder(DataBindingComponent component, View[] views, int layoutId) {
  if(views == null || views.length == 0) {
    return null;
  }
  int localizedLayoutId = INTERNAL_LAYOUT_ID_LOOKUP.get(layoutId);
  if(localizedLayoutId > 0) {
    final Object tag = views[0].getTag();
    if(tag == null) {
      throw new RuntimeException("view must have a tag");
    }
    switch(localizedLayoutId) {
    }
  }
  return null;
}
(6)ActivityMainBindingImpl的构造器

在new出ActivityMainBindingImpl对象后,则进行一些View的绑定操作,将通过tag取出的View与ActivityMainBindingImpl中对应的View属性进行绑定。

public ActivityMainBindingImpl(@Nullable androidx.databinding.DataBindingComponent bindingComponent, @NonNull View root) {
    this(bindingComponent, root, mapBindings(bindingComponent, root, 3, sIncludes, sViewsWithIds));
}

在这里,会调用了一个mapBindings方法,第三个参数是一个3,这个3的意思,就是activity_main.xml布局文件中有3个节点

mapBindings(bindingComponent, root, 3, sIncludes, sViewsWithIds)

mapBindings就会返回一个Object[] bindings数组。

(7)ViewDataBinding.mapBinding()

这里的主要工作,就是将布局中的View保存在对应的bindings数组中,然后取出这个数组中的数据赋值给ActivityMainBindingImpl中的View

protected static Object[] mapBindings(DataBindingComponent bindingComponent, View root,
        int numBindings, IncludedLayouts includes, SparseIntArray viewsWithIds) {
    Object[] bindings = new Object[numBindings];
    mapBindings(bindingComponent, root, bindings, includes, viewsWithIds, true);
    return bindings;
}

private static void mapBindings(DataBindingComponent bindingComponent, View view,
        Object[] bindings, IncludedLayouts includes, SparseIntArray viewsWithIds,
        boolean isRoot) {
    final int indexInIncludes;
    // 判断View是否已经存在绑定,如果已经绑定,则直接return
    final ViewDataBinding existingBinding = getBinding(view);
    if (existingBinding != null) {
        return;
    }
    // 获取View的tag标签
    Object objTag = view.getTag();
    final String tag = (objTag instanceof String) ? (String) objTag : null;
    boolean isBound = false;
    // 如果tag是根布局,并且是以layout开头的tag
    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);
            // 将根布局标签对应的View放在bindings数组中
            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);
            }
        }
    }
}

ActivityMainBindingImpl的父类ActivityMainBinding是在Eapp\build\generated\data_binding_base_class_source_out\debug\out\com\example\databindingdemo\databinding包下

public abstract class ActivityMainBinding extends ViewDataBinding {
  @NonNull
  public final TextView tv1;

  @NonNull
  public final TextView tv2;

  @Bindable
  protected User mUser;

  protected ActivityMainBinding(Object _bindingComponent, View _root, int _localFieldCount,
      TextView tv1, TextView tv2) {
    super(_bindingComponent, _root, _localFieldCount);
    this.tv1 = tv1;
    this.tv2 = tv2;
  }
  ...
}

3.binding.setUser(user)解析

BR文件

BR的作用:其实BR的作用,就用BR中的属性值来标记不同的操作需要的监听在mLocalFieldObservers数组中的位置

// binding.setVariable(BR.name,"21341234123");
// binding.setUser(user);
// 这里的BR,其实代表的含义不同,如果是BR.user,则也是会修改整个User数据
// 如果是binding.setUser(),此时就是修改了整个DataBinding的数据
// 如果是BR.name,则是修改了name的数据
public class BR {
  public static final int _all = 0;

  public static final int name = 1;

  public static final int pwd = 2;

  public static final int user = 3;
}
(1)ActivityMainBindingImpl.setUser()
public void setUser(@Nullable com.example.databindingdemo_20210117.User User) {
    // 更新注册
    updateRegistration(0, User);
    this.mUser = User;
    synchronized(this) {
        mDirtyFlags |= 0x1L;
    }
    notifyPropertyChanged(BR.user);
    super.requestRebind();
}
(2)ViewDataBinding.updateRegistration()
// 使用BR的属性的值做index,存储每个BR的属性对应的监听器
private WeakListener[] mLocalFieldObservers;

// 创建属性的监听器
private static final CreateWeakListener CREATE_PROPERTY_LISTENER = new CreateWeakListener() {
    @Override
    public WeakListener create(ViewDataBinding viewDataBinding, int localFieldId) {
        // 返回一个属性监听
        return new WeakPropertyListener(viewDataBinding, localFieldId).getListener();
    }
};

protected boolean updateRegistration(int localFieldId, Observable observable) {
    return updateRegistration(localFieldId, observable, CREATE_PROPERTY_LISTENER);
}

这里的localFieldId=0,这个id其实就BR文件中的id,就是BR文件中对应的静态final属性的值。而第二个就是观察者对象,比如传入的ViewModel对象。

private boolean updateRegistration(int localFieldId, Object observable,
        CreateWeakListener listenerCreator) {
    if (observable == null) {
        return unregisterFrom(localFieldId);
    }
    // 根据BR的每个属性的属性值做index,存储每个BR属性对应的监听器
    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方法,其实就是将Activity这个观察者和User这个被观察者
// 统一添加到ObservableReference中
protected void registerTo(int localFieldId, Object observable,
        CreateWeakListener listenerCreator) {
    if (observable == null) {
        return;
    }
    WeakListener listener = mLocalFieldObservers[localFieldId];
    if (listener == null) {
        // 通过属性监听器的创建器创建一个BR属性值对应的监听器
        // listener就是WeakPropertyListener对象的getListener返回的
        // 就是WeakListener对象
        // 在创建WeakPropertyListener的时候,其构造器内部就会创建
        // WeakListener对象,并且将WeakPropertyListener对象传给WeakListener
        // 这样WeakListener和WeakPropertyListener就相互持有
        listener = listenerCreator.create(this, localFieldId);
        mLocalFieldObservers[localFieldId] = listener;
        // 将监听器与观察者做绑定,这里的观察者其实就是Activity
        if (mLifecycleOwner != null) {
            listener.setLifecycleOwner(mLifecycleOwner);
        }
    }
    // 将该监听器与被观察者做绑定
    // 比如这里的被观察者就是User
    listener.setTarget(observable);
}

这里通过WeakListener监听器中的ObservableReference对象保存观察者与被观察者,当被观察者发生改变的时候,就会找到对应的WeakListener监听器,然后通知观察者做修改。
而ObservableReference方法的实现,有多个,比如:WeakPropertyListener。
这里让WeakListener.setTarget()其实就是通过WeakPropertyListener给被观察者添加callback,然后当被观察者数据发生改变的时候,被观察者通过遍历其内部的PropertyChangeRegistry中的OnPropertyChangedCallback回调(其实就是WeakPropertyListener),然后通过WeakPropertyListener监听通知给ViewDataBinding以及其实现类ActivityMainBindingImpl具体进行数据的处理和设置。

(3)WeakListener.setTarget()给Observable添加回调

// 这里的mTarget其实是一个泛型T对象,而这个泛型是在WeakPropertyListener初始化WeakListener的时候传入的一个Observable,这个是databinding中的Observable,其子类实现就是BaseObservable

// ViewDataBinding.java
// 这里的mObservable就是WeakPropertyListener对象
private final ObservableReference<T> mObservable;

public void setTarget(T object) {
    unregister();
    mTarget = object;
    if (mTarget != null) {
        mObservable.addListener(mTarget);
    }
}

WeakPropertyListener中的addListener方法,就会给Observable添加一个callback回调,向Observable这个被观察者中添加callback的目的,就是在Observable数据发生变化的时候,遍历Observable中的mCallbacks这个callback集合,通知观察者进行修改。

// ViewDataBinding.java
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) {
        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?
        }
        binder.handleFieldChange(mListener.mLocalFieldId, sender, propertyId);
    }
}
// BaseObservable.java
@Override
public void addOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
    synchronized (this) {
        if (mCallbacks == null) {
            // 这个PropertyChangeRegistry就是用来做属性修改的时候的回调的
            // 因为往被观察者中添加了callback
            // 当被观察者发生修改的时候,就会遍历这个mCallbacks进行数据回调
            mCallbacks = new PropertyChangeRegistry();
        }
    }
    mCallbacks.add(callback);
}
// PropertyChangeRegistry.java
public class PropertyChangeRegistry extends
        CallbackRegistry<Observable.OnPropertyChangedCallback, Observable, Void> {

    private static final CallbackRegistry.NotifierCallback<Observable.OnPropertyChangedCallback, Observable, Void> NOTIFIER_CALLBACK = new CallbackRegistry.NotifierCallback<Observable.OnPropertyChangedCallback, Observable, Void>() {
        @Override
        public void onNotifyCallback(Observable.OnPropertyChangedCallback callback, Observable sender,
                int arg, Void notUsed) {
            callback.onPropertyChanged(sender, arg);
        }
    };

    public PropertyChangeRegistry() {
        // 将静态属性NOTIFIER_CALLBACK设置给其父类的mNotifier
        // 设置这个的目的,就是在被观察者数据发生改好的时候
        // 能通过NOTIFIER_CALLBACK对象实现中的onNotifyCallback方法
        // 接收回调处理,将处理过程回调给WeakPropertyListener
        super(NOTIFIER_CALLBACK);
    }

    /**
     * Notifies registered callbacks that a specific property has changed.
     *
     * @param observable The Observable that has changed.
     * @param propertyId The BR id of the property that has changed or BR._all if the entire
     *                   Observable has changed.
     */
    public void notifyChange(@NonNull Observable observable, int propertyId) {
        notifyCallbacks(observable, propertyId, null);
    }
}

从这第三步可以知道:

  • BaseObservable这个被观察者,会被WeakListener持有,而WeakListener会被WeakPropertyListener持有,从而BaseObservable会间接的被WeakPropertyListener持有。
  • 而BaseObservable会通过mCallbacks持有Observable.OnPropertyChangedCallback对象,而Observable.OnPropertyChangedCallback的子类是WeakPropertyListener,所以BaseObservable也会持有WeakPropertyListener对象
  • 而BaseObservable会持有一个mCallbacks对象,这是一个PropertyChangeRegistry对象,是CallbackRegistry的子类

而WeakPropertyListener和WeakListener是相互持有的对方的引用。

在完成监听的相互绑定关系,并且给Observable添加了回调之后,就会回到ActivityMainBindingImpl的setUser()方法继续执行notifyPropertyChanged()方法。
但是这里的例子有个问题,就是监听是添加在User这个BaseObservable的子类中的,但是更新的时候,并不是通过这个User来进行通知,而是根据ActivityMainBindingImpl这个BaseObservable来通知,那么这个时候并不会通过ActivityMainBindingImpl的调用notifyPropertyChanged()最终拿到User中的PropertyChangeRegistry对象mCallbacks,所以起作用的并不是这句话。而最终ActivityMainBindingImpl在设置User起刷新作用,是因为super.requestRebind()的调用也触发了mRebindRunnable任务的执行,其实就是没有通过PropertyChange来触发requestRebind()

(4)ActivityMainBindingImpl.notifyPropertyChanged()

这里其实就是调用的BaseObservable的notifyPropertyChanged()方法,因为ActivityMainBindingImpl是ViewDataBinding的子类,而ViewDataBinding继承了BaseObservable类

// ViewDataBinding.java
public void notifyPropertyChanged(int fieldId) {
    synchronized (this) {
        if (mCallbacks == null) {
            return;
        }
    }
    mCallbacks.notifyCallbacks(this, fieldId, null);
}
// CallbackRegistry.java
public synchronized void notifyCallbacks(T sender, int arg, A arg2) {
    mNotificationLevel++;
    notifyRecurse(sender, arg, arg2);
    mNotificationLevel--;
    if (mNotificationLevel == 0) {
        if (mRemainderRemoved != null) {
            for (int i = mRemainderRemoved.length - 1; i >= 0; i--) {
                final long removedBits = mRemainderRemoved[i];
                if (removedBits != 0) {
                    removeRemovedCallbacks((i + 1) * Long.SIZE, removedBits);
                    mRemainderRemoved[i] = 0;
                }
            }
        }
        if (mFirst64Removed != 0) {
            removeRemovedCallbacks(0, mFirst64Removed);
            mFirst64Removed = 0;
        }
    }
}

private void notifyRecurse(T sender, int arg, A arg2) {
    final int callbackCount = mCallbacks.size();
    final int remainderIndex = mRemainderRemoved == null ? -1 : mRemainderRemoved.length - 1;

    // Now we've got all callbakcs that have no mRemainderRemoved value, so notify the
    // others.
    notifyRemainder(sender, arg, arg2, remainderIndex);

    // notifyRemainder notifies all at maxIndex, so we'd normally start at maxIndex + 1
    // However, we must also keep track of those in mFirst64Removed, so we add 2 instead:
    final int startCallbackIndex = (remainderIndex + 2) * Long.SIZE;

    // The remaining have no bit set
    notifyCallbacks(sender, arg, arg2, startCallbackIndex, callbackCount, 0);
}


private void notifyRemainder(T sender, int arg, A arg2, int remainderIndex) {
    if (remainderIndex < 0) {
        notifyFirst64(sender, arg, arg2);
    } else {
        final long bits = mRemainderRemoved[remainderIndex];
        final int startIndex = (remainderIndex + 1) * Long.SIZE;
        final int endIndex = Math.min(mCallbacks.size(), startIndex + Long.SIZE);
        notifyRemainder(sender, arg, arg2, remainderIndex - 1);
        notifyCallbacks(sender, arg, arg2, startIndex, endIndex, bits);
    }
}


private void notifyFirst64(T sender, int arg, A arg2) {
    final int maxNotified = Math.min(Long.SIZE, mCallbacks.size());
    notifyCallbacks(sender, arg, arg2, 0, maxNotified, mFirst64Removed);
}


private void notifyCallbacks(T sender, int arg, A arg2, final int startIndex,
        final int endIndex, final long bits) {
    long bitMask = 1;
    for (int i = startIndex; i < endIndex; i++) {
        if ((bits & bitMask) == 0) {
            // mCallbacks是PropertyChangeRegistry对象
            // mCallbacks持有的就是WeakPropertyListener对象
            // 这里其实就是遍历执行所有的WeakPropertyListener的onPropertyChanged方法
            // mNotifier是静态对象,是由PropertyChangeRegistry中创建
            // 并且由其构造器中传给其PropertyChangeRegistry的父类
            mNotifier.onNotifyCallback(mCallbacks.get(i), sender, arg, arg2);
        }
        bitMask <<= 1;
    }
}

这里的mNotifier.notifyCallback其实就会调用到下面的PropertyChangeRegistry中定义的NOTIFIER_CALLBACK 属性中的onNotifyCallback实现,而这里的callback其实就是WeakPropertyListener,因为WeakPropertyListener是OnPropertyChangedCallback的子类,这里其实会回调给mLocalFieldObservers数组中所有的WeakListener

public class PropertyChangeRegistry extends CallbackRegistry<OnPropertyChangedCallback, Observable, Void> {
    private static final NotifierCallback<OnPropertyChangedCallback, Observable, Void> NOTIFIER_CALLBACK = new NotifierCallback<OnPropertyChangedCallback, Observable, Void>() {
        public void onNotifyCallback(OnPropertyChangedCallback callback, Observable sender, int arg, Void notUsed) {
            // WeakPropertyListener是OnPropertyChangedCallback的子类
            callback.onPropertyChanged(sender, arg);
        }
    };

    public PropertyChangeRegistry() {
        super(NOTIFIER_CALLBACK);
    }

    public void notifyChange(@NonNull Observable observable, int propertyId) {
        this.notifyCallbacks(observable, propertyId, (Object)null);
    }
}
// ViewDataBinding.java中的WeakPropertyListener类
@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?
    }
    // 从mListener中取出target,而这里的mListener其实就是
    // WeakListener,而每个被观察者,其实都是有一个对应的LocalFieldId
    // 这个id就是BR文件中定义的,刚才的流程中,我们传入的是0
    // 所以这里的mLocalFieldId=0
    binder.handleFieldChange(mListener.mLocalFieldId, sender, propertyId);
}

从mListener中取出target,而这里的mListener其实就是,WeakListener,而每个被观察者,其实都是有一个对应的LocalFieldId,这个id就是BR文件中定义的,刚才的流程中,我们传入的是0,所以这里的mLocalFieldId=0

// ViewDataBinding.java
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;
    }
    // mLocalFieldId=0
    boolean result = onFieldChange(mLocalFieldId, object, fieldId);
    if (result) {
        requestRebind();
    }
}

这里的onFieldChange的方法的实现,就是在ActivityMainBindingImpl.java中

// ActivityMainBindingImpl.java
@Override
protected boolean onFieldChange(int localFieldId, Object object, int fieldId) {
    switch (localFieldId) {
        case 0 :
            return onChangeUser((com.example.databindingdemo_20210117.User) object, fieldId);
    }
    return false;
}

private boolean onChangeUser(com.example.databindingdemo_20210117.User User, int fieldId) {
    // mDirtyFlags的初始值 0xffffffffffffffffL
    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.pwd) {
        synchronized(this) {
                mDirtyFlags |= 0x4L;
        }
        return true;
    }
    return false;
}

这里因为fieldId=0,所以会进入第一个if条件if (fieldId == BR._all),所以会返回true,所以就会返回到ViewDataBinding.java中的handleFieldChange方法中,继续执行requestRebind()

// ViewDataBinding.java
protected void requestRebind() {
    if (mContainingBinding != null) {
        mContainingBinding.requestRebind();
    } else {
        final LifecycleOwner owner = this.mLifecycleOwner;
        if (owner != null) {
            Lifecycle.State state = owner.getLifecycle().getCurrentState();
            if (!state.isAtLeast(Lifecycle.State.STARTED)) {
                return; // wait until lifecycle owner is started
            }
        }
        synchronized (this) {
            if (mPendingRebind) {
                return;
            }
            mPendingRebind = true;
        }
        if (USE_CHOREOGRAPHER) {
            mChoreographer.postFrameCallback(mFrameCallback);
        } else {
            mUIThreadHandler.post(mRebindRunnable);
        }
    }
}

这里最终都会执行mRebindRunnable的run()方法。只不过SDK版本大于等于16的时候,会采用Choreographer编舞者来处理,而之前的版本则是采用Handler来执行。

// ViewDataBinding.java
private final Runnable mRebindRunnable = new Runnable() {
    @Override
    public void run() {
        synchronized (this) {
            mPendingRebind = false;
        }
        processReferenceQueue();

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            // Nested so that we don't get a lint warning in IntelliJ
            if (!mRoot.isAttachedToWindow()) {
                // Don't execute the pending bindings until the View
                // is attached again.
                mRoot.removeOnAttachStateChangeListener(ROOT_REATTACHED_LISTENER);
                mRoot.addOnAttachStateChangeListener(ROOT_REATTACHED_LISTENER);
                return;
            }
        }
        executePendingBindings();
    }
};

public void executePendingBindings() {
    if (mContainingBinding == null) {
        executeBindingsInternal();
    } else {
        mContainingBinding.executePendingBindings();
    }
}

private void executeBindingsInternal() {
    if (mIsExecutingPendingBindings) {
        requestRebind();
        return;
    }
    if (!hasPendingBindings()) {
        return;
    }
    mIsExecutingPendingBindings = true;
    mRebindHalted = false;
    if (mRebindCallbacks != null) {
        mRebindCallbacks.notifyCallbacks(this, REBIND, null);

        // The onRebindListeners will change mPendingHalted
        if (mRebindHalted) {
            mRebindCallbacks.notifyCallbacks(this, HALTED, null);
        }
    }
    if (!mRebindHalted) {
        executeBindings();
        if (mRebindCallbacks != null) {
            mRebindCallbacks.notifyCallbacks(this, REBOUND, null);
        }
    }
    mIsExecutingPendingBindings = false;
}

在这里最终就会执行到executeBindings()方法,而该方法的实现,又是在ActivityMainBindingImpl.java中

// ActivityMainBindingImpl.java
protected void executeBindings() {
    long dirtyFlags = 0;
    synchronized(this) {
        // mDirtyFlags在之前的onChangeUser方法中
        // 因为fieldId=0,所以会执行mDirtyFlags |= 0x1L
        // 而初始值为private  long mDirtyFlags = 0xffffffffffffffffL
        dirtyFlags = mDirtyFlags;
        mDirtyFlags = 0;
    }
    java.lang.String userName = null;
    com.example.databindingdemo_20210117.User user = mUser;
    java.lang.String userPwd = null;
    // 因为mDirtyFlags或了一个0x1L,此时再执行一个与操作
    // 不为0
    if ((dirtyFlags & 0xfL) != 0) {

        // 0xbL为1011,与上0001,则不会为0
        if ((dirtyFlags & 0xbL) != 0) {

                if (user != null) {
                    // read user.name
                    userName = user.getName();
                }
        }
        // 0xdL=1101,与上0001,也不会为0
        if ((dirtyFlags & 0xdL) != 0) {

                if (user != null) {
                    // read user.pwd
                    userPwd = user.getPwd();
                }
        }
    }
    // batch finished
    // 这里最终再执行setText的操作,其实就是调用了页面布局的View的setText
    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, userPwd);
    }
}

三、总结

1.实现了BaseObservable的类数据更新

如果自定义类继承了BaseObservable类,则会更新注册监听。即BaseObservable保存PropertyChangeRegistry对象,该对象中会保存WeakPropertyListener监听,而WeakPropertyListener监听会持有WeakListener,WeakListener也会持有WeakPropertyListener,并且持有一个BaseObservable的target,这个target就是自定义的BaseObservable子类实现对象,在设置target的时候就会将WeakPropertyListener监听给保存在这个target中的PropertyChangeRegistry对象中,当使用自定义的BaseObservable进行更新的时候,就可以通过监听回调的方式通知到ActivityMainBindingImpl这些ViewDataBinding中,然后向ActivityMainBindingImpl解析得到的View实体中设置对应的数据。
通过WeakListener监听器中的ObservableReference对象保存观察者与被观察者,当被观察者发生改变的时候,就会找到对应的WeakListener监听器,然后通知观察者做修改。
而ObservableReference方法的实现,有多个,比如:WeakPropertyListener。
这里让WeakListener.setTarget()其实就是通过WeakPropertyListener给被观察者添加callback,然后当被观察者数据发生改变的时候,被观察者通过遍历其内部的PropertyChangeRegistry中的OnPropertyChangedCallback回调(其实就是WeakPropertyListener),然后通过WeakPropertyListener监听通知给ViewDataBinding以及其实现类ActivityMainBindingImpl具体进行数据的处理和设置。
其实就是向ViewModel或者自定义的Observable(是databinding中的Observable)的子类实现中的mCallbacks中添加监听WeakPropertyListener,用于数据变化回调。而在WeakPropertyListener中的WeakListener对象会保存这个Observable为target,用于在更新的时候取出。

2.一般数据更新

比如在xml布局的data中直接使用Boolean、Integer、String等类型
如果ActivityMainBindingImpl中设置的是比如ViewModel,那么就需要看是否使用了LiveData修饰的属性,如果没有使用LiveData的,则并不会去更新注册监听信息,而只是重新保存ViewDataBinding中保存的数据实体,并且直接调用ActivityMainBinding中保存的View实体进行设置新的数据

3.LiveData数据更新

如果是使用了LiveData的话,则会在调用LiveDataListener(这是一个Observer子类与上面的WeakPropertyListener类似)的addListener的时候,就会给LiveData注册观察者LiveDataListener,然后在LiveDataListener中的onChanged实现中通过调用ViewDataBinding的handleFieldChange方法触发数据变化修改,进而更新View显示的数据

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容