在RecyclerView中实现定点刷新

96
ditclear
1.3 2019.05.27 18:45* 字数 1040

本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布

写在前面

近期,在笔者开源的BindingListAdapter库中出现了这样的一个Issue。

这其实是一个在列表中比较常见的问题,在没有找到比较好的解决办法之前,确实都是通过整项刷新notifyDataChanged来保证数据显示的正确性。到后来的notifyItemChanged和更佳的DiffUtil,说明开发者们一直都在想办法来解决并优化它。

但其实如果你使用DataBinding,做这个局部刷新或者说是定点刷新,就很简单了,这可能是大多数使用DataBinding的开发者并不知道的技巧。

巧用ObservableFiled

可以先看看实际的效果

关键点就在于不要直接绑定具体的值到xml中,应先使用ObservableField包裹一层。

class ItemViewModel constructor( val data:String){
    //not
    //val count = data
    //val liked = false
    
    //should
    val count = ObservableField<String>(data)
    val liked = ObservableBoolean()
}

//partial_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="item"
            type="io.ditclear.app.partial.PartialItemViewModel" />

        <variable
            name="presenter"
            type="io.ditclear.bindingadapterx.ItemClickPresenter" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:onClick="@{(v)->presenter.onItemClick(v,item)}"
        >
        <TextView
            android:text="@{item.count}" />
        <ImageView
            android:src="@{item.liked?@drawable/ic_action_liked:@drawable/ic_action_unlike}"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

当UI需要改变时,改变ItemViewModel的数据即可。

//activity

override fun onItemClick(v: View, item: PartialItemViewModel) {
    item.toggle()
}

// ItemViewModle
 fun toggle(){
        liked.set(!liked.get())

        count.set("$data ${if (liked.get()) "liked" else ""}")
    }

至于为什么?这就要说到DataBinding更新UI的原理了。

DataBinding更新UI原理

当我们在项目中运用DataBinding,并将xml文件转换为DataBinding形式之后。经过编译build,会生成相应的binding文件,比如partial_list_item.xml就会生成对于的PartialListItemBinding文件,这是一个抽象类,还会有一个PartialListItemBindingImpl实现类实现具体的渲染UI的方法executeBindings

当数据有改变的时候,就会重新调用executeBindings方法,更新UI,那怎么做到的呢?

我们先来看PartialListItemBinding的构造方法.

public abstract class PartialListItemBinding extends ViewDataBinding {
  @NonNull
  public final ImageView imageView;

  @Bindable
  protected PartialItemViewModel mItem;

  @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("DataBinding 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<Observable> {
    final WeakListener<Observable> mListener;

    public WeakPropertyListener(ViewDataBinding binder, int localFieldId) {
        mListener = new WeakListener<Observable>(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 解析

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

/**
 * Runnable executed on animation heartbeat to rebind the dirty Views.
 */
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();
    }
};

其实就是在下一帧的时候再执行了一次executePendingBindings方法,到这里,DataBinding更新UI的逻辑我们也就全部打通了。

写在最后

笔者已经使用了DataBinding好几年的时间,深切的体会到了它对于开发效率的提升,决不下于Kotlin,用好了它就是剑客最锋利的宝剑,削铁如泥,用不好便自损八百。为此,上一年我专门写了一篇DataBinding实用指南讲了讲自己的使用经验,同时开源了运用DataBinding来简化RecyclerView适配器的BindingListAdapter,希望更多的开发者喜欢它。

GitHub示例:https://github.com/ditclear/BindingListAdapter

参考资料

深入Android databinding的使用和原理分析

Choreographer 解析

==================== 分割线 ======================

如果你想了解更多关于MVVM、Flutter、响应式编程方面的知识,欢迎关注我。

你可以在以下地方找到我:

简书:https://www.jianshu.com/u/117f1cf0c556

掘金:https://juejin.im/user/582d601d2e958a0069bbe687

Github: https://github.com/ditclear

android