DataBinding(二)

Android知识总结

一、set 解析

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

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

ViewDataBinding#registerTo

    // 这个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具体进行数据的处理和设置。

二、绑定 Lifecycle

ViewDataBinding#setLifecycleOwner

    public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {
        if (mLifecycleOwner == lifecycleOwner) {
            return;
        }
        //当存在观察者移出,然后会添加新的观察者
        if (mLifecycleOwner != null) {
            mLifecycleOwner.getLifecycle().removeObserver(mOnStartListener);
        }
        mLifecycleOwner = lifecycleOwner;
        if (lifecycleOwner != null) {
            if (mOnStartListener == null) {
                //创建观察者
                mOnStartListener = new OnStartListener(this);
            }
            lifecycleOwner.getLifecycle().addObserver(mOnStartListener);
        }
        for (WeakListener weakListener : mLocalFieldObservers) {
            if (weakListener != null) {
                weakListener.setLifecycleOwner(lifecycleOwner);
            }
        }
    }

OnStartListener继承于LifecycleObserverViewDataBinding的内部类,主要用于观察生命周期变化。

    static class OnStartListener implements LifecycleObserver {
        //弱引用 ViewDataBinding
        final WeakReference mBinding;
        private OnStartListener(ViewDataBinding binding) {
            mBinding = new WeakReference<>(binding);
        }
        //start 状态
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onStart() {
            ViewDataBinding dataBinding = mBinding.get();
            if (dataBinding != null) {
                dataBinding.executePendingBindings();
            }
        }
    }

你可能感兴趣的:(DataBinding(二))