zoukankan      html  css  js  c++  java
  • LiveEventBus

    先简单看看 LiveEventBus 是怎么使用 LiveData 的。

    public final class LiveEventBus {

    public synchronized <T> Observable<T> with(String key, Class<T> type) {
    if (!bus.containsKey(key)) {
    bus.put(key, new LiveEvent<>(key));
    }
    return (Observable<T>) bus.get(key);
    }

    private class LiveEvent<T> implements Observable<T> {

    private final LifecycleLiveData<T> liveData;

    LiveEvent(@NonNull String key) {
    this.key = key;
    this.liveData = new LifecycleLiveData<>();
    }

    @Override
    public void observe(@NonNull final LifecycleOwner owner, @NonNull final Observer<T> observer) {
    if (ThreadUtils.isMainThread()) {
    observeInternal(owner, observer);
    } else {
    mainHandler.post(new Runnable() {
    @Override
    public void run() {
    observeInternal(owner, observer);
    }
    });
    }
    }

    @MainThread
    private void observeInternal(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
    ObserverWrapper<T> observerWrapper = new ObserverWrapper<>(observer);
    observerWrapper.preventNextEvent = liveData.getVersion(http://www.amjmh.com/v/BIBRGZ_558768/) > ExternalLiveData.START_VERSION;
    liveData.observe(owner, observerWrapper);
    }

    @Override
    public void post(T value) {
    if (ThreadUtils.isMainThread()) {
    postInternal(value);
    } else {
    mainHandler.post(new PostValueTask(value));
    }
    }

    @MainThread
    private void postInternal(T value) {
    liveData.setValue(value);
    }

  • 相关阅读:
    旋转数组的最小值
    用堆栈实现队列
    二叉树的重建
    从尾到头打印链表
    实现替换空格
    java获取在各种编码下中文及英文的字符个数
    java定义一个二维数组
    java计算某个坐标是否在范围内
    java调用百度地图API
    map在遍历数据的过程中删除数据不出错
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11384644.html
Copyright © 2011-2022 走看看