我是靠谱客的博主 清爽花卷,最近开发中收集的这篇文章主要介绍DataBinding UI更新原理解析ObservableLiveData绑定,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

  • Observable
    • BaseObservable
    • PropertyChangeRegistry
    • WeakPropertyListener
    • handleFieldChange
    • requestRebind
    • mRebindRunnable
    • executeBindings(仅更新mDirtyFlags标识)
  • LiveData
    • LiveData.setValue()
    • dispatchingValue
    • considerNotify
    • onChanged
    • handleFieldChange
    • requestRebind->mRebindRunnable->executeBindings
  • 绑定
    • UI->Data
    • Data->UI(executeBindings中执行)

Observable

BaseObservable

private transient PropertyChangeRegistry mCallbacks;

PropertyChangeRegistry

        callback.onPropertyChanged(sender, arg);

WeakPropertyListener

    public void onPropertyChanged(Observable sender, int propertyId) {
        ViewDataBinding binder = this.mListener.getBinder();
        if (binder != null) {
            Observable obj = (Observable)this.mListener.getTarget();
            if (obj == sender) {
                binder.handleFieldChange(this.mListener.mLocalFieldId, sender, propertyId);
            }
        }
    }

handleFieldChange

private void handleFieldChange(int mLocalFieldId, Object object, int fieldId) {
    if (!this.mInLiveDataRegisterObserver) {
        boolean result = this.onFieldChange(mLocalFieldId, object, fieldId);
        if (result) {
            this.requestRebind();
        }

    }
}

requestRebind

        if (USE_CHOREOGRAPHER) {
            this.mChoreographer.postFrameCallback(this.mFrameCallback);
        } else {
            this.mUIThreadHandler.post(this.mRebindRunnable);
        }

mRebindRunnable

        ViewDataBinding.this.executePendingBindings();

executeBindings(仅更新mDirtyFlags标识)

LiveData

LiveData.setValue()

dispatchingValue(null);

dispatchingValue

considerNotify(initiator);

considerNotify

observer.mObserver.onChanged((T) mData);

onChanged

    @Override
    public void onChanged(@Nullable Object o) {
        ViewDataBinding binder = mListener.getBinder();
        if (binder != null) {
            binder.handleFieldChange(mListener.mLocalFieldId, mListener.getTarget(), 0);
        }
    }

handleFieldChange

onFieldChange();//设置mDirtyFlags
requestRebind();

requestRebind->mRebindRunnable->executeBindings

绑定

UI->Data

setOnCheckedChangeListener(androidx.databinding.InverseBindingListener)
回调中设置 observable对象的值,此时不更新UI也不执行executeBindings;

Data->UI(executeBindings中执行)

最后依据observable的最新变化,设置view的属性
setChecked(view, value);

最后

以上就是清爽花卷为你收集整理的DataBinding UI更新原理解析ObservableLiveData绑定的全部内容,希望文章能够帮你解决DataBinding UI更新原理解析ObservableLiveData绑定所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部