栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

分享一个RecyclerView中定点刷新的小技巧,kotlinlambda表达式的脱离

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

分享一个RecyclerView中定点刷新的小技巧,kotlinlambda表达式的脱离

@Bindable

protected ItemClickPresenter mPresenter;

protected PartialListItemBinding(DataBindingComponent _bindingComponent, View _root,

int _localFieldCount, ImageView imageView) {

//调用父类的构造方法

super(_bindingComponent, _root, _localFieldCount);

this.imageView = imageView;

}

//…

}

调用了父类ViewDataBinding的构造方法,并传入了三个参数,这里看第三个参数_localFieldCount,它代表xml中存在几个ObservableField形式的数据,继续追踪.

protected ViewDataBinding(DataBindingComponent bindingComponent, View root, int localFieldCount) {

this.mBindingComponent = bindingComponent;

//考点1

this.mLocalFieldObservers = new ViewDataBinding.WeakListener[localFieldCount];

this.mRoot = root;

if (Looper.myLooper() == null) {

throw new IllegalStateException(“Dat
aBinding must be created in view’s UI Thread”);

} else {

if (USE_CHOREOGRAPHER) {

//考点2

this.mChoreographer = Choreographer.getInstance();

this.mframeCallback = new frameCallback() {

public void doframe(long frameTimeNanos) {

ViewDataBinding.this.mRebindRunnable.run();

}

};

} else {

this.mframeCallback = null;

this.mUIThreadHandler = new Handler(Looper.myLooper());

}

}

}

通过观察,发现其根据localFieldCount初始化了一个WeakListener数组,名为mLocalFieldObservers。另一个重点是初始化了一个mframeCallback,在回调中执行了mRebindRunnable.run。

当生成的PartialListItemBindingImpl对象调用executeBindings方法时,通过updateRegistration会对mLocalFieldObservers数组中的内容进行赋值。

随之生成的是相应的WeakPropertyListener,来看看它的定义。

private static class WeakPropertyListener extends Observable.OnPropertyChangedCallback

implements ObservableReference {

final WeakListener mListener;

public WeakPropertyListener(ViewDataBinding binder, int localFieldId) {

mListener = new WeakListener(binder, localFieldId, this);

}

//…

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

}

}

当ObservableField的值有改变的时候,onPropertyChanged会被调用,然后就会回调binder(即binding对象)的handleFieldChange方法,继续观察。

private void handleFieldChange(int mLocalFieldId, Object object, int fieldId) {

if (!this.mInLiveDataRegisterObserver) {

boolean result = this.onFieldChange(mLocalFieldId, object, fieldId);

if (result) {

this.requestRebind();

}

}

}

如果值有改变 ,result为true,接着requestRebind方法被执行。

protected void requestRebind() {

if (this.mContainingBinding != null) {

this.mContainingBinding.requestRebind();

} else {

synchronized(this) {

if (this.mPendingRebind) {

return;

}

this.mPendingRebind = true;

}

if (this.mLifecycleOwner != null) {

State state = this.mLifecycleOwner.getLifecycle().getCurrentState();

if (!state.isAtLeast(State.STARTED)) {

return;

}

}

//划重点

if (USE_CHOREOGRAPHER) { // SDK_INT >= 16

this.mChoreographer.postframeCallback(this.mframeCallback);

} else {

this.mUIThreadHandler.post(this.mRebindRunnable);

}

}

}

在上述代码最后,可以看到sdk版本16以上会执行

this.mChoreographer.postframeCallback(this.mframeCallback);,16以下则是通过Handler。

关于postframeCallBack,给的注释是Posts a frame callback to run on the next frame.,简单理解就是发生在下一帧即16ms之后的回调。

关于Choreographer,推荐阅读** Choreographer 解析**

https://www.jianshu.com/p/dd32ec35db1d

但不管如何,最终都是调用mRebindRunnable.run,来看看对它的定义。

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;

}

}

//划重点

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

}

}

//划重点

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710005.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号