liveData = new MutableLiveData<>();
liveData.observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable final String newText) {
// 更新數據
tv3.setText(newText);
}
});
liveData.setValue("小楊真的是一個逗比麼");
複製代碼
public class TextViewModel extends ViewModel {
/**
* LiveData是抽象類,MutableLiveData是具體實現類
*/
private MutableLiveData<String> mCurrentText;
public MutableLiveData<String> getCurrentText() {
if (mCurrentText == null) {
mCurrentText = new MutableLiveData<>();
}
return mCurrentText;
}
}
複製代碼
private void initLiveData() {
// 建立一個持有某種數據類型的LiveData (一般是在ViewModel中)
model = ViewModelProviders.of(this).get(TextViewModel.class);
// 建立一個定義了onChange()方法的觀察者
// 開始訂閱
final Observer<String> nameObserver = new Observer<String>() {
@Override
public void onChanged(@Nullable final String newText) {
// 更新數據
tvText.setText(newText);
}
};
// 經過 observe()方法鏈接觀察者和LiveData,注意:observe()方法須要攜帶一個LifecycleOwner類
model.getCurrentText().observe(this, nameObserver);
}
複製代碼
findViewById(R.id.tv_click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
String text;
switch (count%5){
case 1:
text = "小楊真的是一個逗比麼";
break;
case 2:
text = "逗比趕忙來star吧";
break;
case 3:
text = "小楊想成爲大神";
break;
case 4:
text = "開始刷新數據啦";
break;
default:
text = "變化成默認的數據";
break;
}
model.getCurrentText().setValue(text);
}
});
複製代碼
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
//當前綁定的組件(activity或者fragment)狀態爲DESTROYED的時候, 則會忽視當前的訂閱請求
if (owner.getLifecycle().getCurrentState() == DESTROYED) {
// ignore
return;
}
//建立生命週期感知的觀察者包裝類
LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
//若是指定的鍵還沒有與某個值關聯,則將其與給定的值關聯
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
//對應觀察者只能與一個owner綁定
if (existing != null && !existing.isAttachedTo(owner)) {
throw new IllegalArgumentException("Cannot add the same observer"
+ " with different lifecycles");
}
if (existing != null) {
return;
}
//lifecycle註冊
//添加一個LifecycleObserver,它將在LifecycleOwner更改狀態時獲得通知
owner.getLifecycle().addObserver(wrapper);
}
複製代碼
class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {
@NonNull final LifecycleOwner mOwner;
LifecycleBoundObserver(@NonNull LifecycleOwner owner, Observer<T> observer) {
super(observer);
mOwner = owner;
}
@Override
boolean shouldBeActive() {
return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);
}
@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
// 當接收到 DESTROYED 的事件會自動解除跟 owner 的綁定
removeObserver(mObserver);
return;
}
activeStateChanged(shouldBeActive());
}
@Override
boolean isAttachedTo(LifecycleOwner owner) {
return mOwner == owner;
}
@Override
void detachObserver() {
mOwner.getLifecycle().removeObserver(this);
}
}
//抽象類
private abstract class ObserverWrapper {
final Observer<T> mObserver;
boolean mActive;
int mLastVersion = START_VERSION;
ObserverWrapper(Observer<T> observer) {
mObserver = observer;
}
abstract boolean shouldBeActive();
boolean isAttachedTo(LifecycleOwner owner) {
return false;
}
void detachObserver() {
}
void activeStateChanged(boolean newActive) {
if (newActive == mActive) {
return;
}
// immediately set active state, so we'd never dispatch anything to inactive // owner mActive = newActive; boolean wasInactive = LiveData.this.mActiveCount == 0; LiveData.this.mActiveCount += mActive ? 1 : -1; if (wasInactive && mActive) { onActive(); } if (LiveData.this.mActiveCount == 0 && !mActive) { onInactive(); } if (mActive) { dispatchingValue(this); } } } //接口 public interface GenericLifecycleObserver extends LifecycleObserver { /** * Called when a state transition event happens. * * @param source The source of the event * @param event The event */ void onStateChanged(LifecycleOwner source, Lifecycle.Event event); } 複製代碼
if (wasInactive && mActive) {
onActive();
}
if (LiveData.this.mActiveCount == 0 && !mActive) {
onInactive();
}
複製代碼
//mObservers是一個集合
private SafeIterableMap<Observer<T>, ObserverWrapper> mObservers =
new SafeIterableMap<>();
//在SafeIterableMap類中的putIfAbsent方法
public V putIfAbsent(@NonNull K key, @NonNull V v) {
Entry<K, V> entry = get(key);
if (entry != null) {
return entry.mValue;
}
put(key, v);
return null;
}
protected Entry<K, V> put(@NonNull K key, @NonNull V v) {
Entry<K, V> newEntry = new Entry<>(key, v);
mSize++;
if (mEnd == null) {
mStart = newEntry;
mEnd = mStart;
return newEntry;
}
mEnd.mNext = newEntry;
newEntry.mPrevious = mEnd;
mEnd = newEntry;
return newEntry;
}
複製代碼
@MainThread
protected void setValue(T value) {
assertMainThread("setValue");
// 這裏的 mVersion,它本問題關鍵,每次更新數據都會自增,默認值是 -1。
mVersion++;
mData = value;
dispatchingValue(null);
}
複製代碼
private void dispatchingValue(@Nullable ObserverWrapper initiator) {
// mDispatchingValue的判斷主要是爲了解決併發調用dispatchingValue的狀況
// 當對應數據的觀察者在執行的過程當中, 若有新的數據變動, 則不會再次通知到觀察者。因此觀察者內的執行不該進行耗時工做
if (mDispatchingValue) {
//給分發失敗打個標記
mDispatchInvalidated = true;
return;
}
// 標記分發開始
mDispatchingValue = true;
do {
mDispatchInvalidated = false;
//這裏須要注意:區分ObserverWrapper對象爲空,和不爲空的邏輯是不同的
if (initiator != null) {
// 等下重點看這裏的代碼
considerNotify(initiator);
initiator = null;
} else {
//能夠發現這裏用到mObservers集合,使用迭代器遍歷數據
for (Iterator<Map.Entry<Observer<T>, ObserverWrapper>> iterator =
mObservers.iteratorWithAdditions(); iterator.hasNext(); ) {
// 等下重點看這裏的代碼
considerNotify(iterator.next().getValue());
if (mDispatchInvalidated) {
break;
}
}
}
} while (mDispatchInvalidated);
// 標記分發開始
mDispatchingValue = false;
}
複製代碼
private void considerNotify(ObserverWrapper observer) {
if (!observer.mActive) {
return;
}
// 檢查最新的狀態b4調度。也許它改變了狀態,但咱們尚未獲得事件。
// 咱們仍是先檢查觀察者。活動,以保持它做爲活動的入口。
// 所以,即便觀察者移動到一個活動狀態,若是咱們沒有收到那個事件,咱們最好不要通知一個更可預測的通知順序。
if (!observer.shouldBeActive()) {
observer.activeStateChanged(false);
return;
}
//注意認真看下面的代碼
if (observer.mLastVersion >= mVersion) {
return;
}
observer.mLastVersion = mVersion;
//noinspection unchecked
observer.mObserver.onChanged((T) mData);
}
複製代碼
private abstract class ObserverWrapper {
final Observer<T> mObserver;
boolean mActive;
int mLastVersion = START_VERSION;
//省略部分代碼
void activeStateChanged(boolean newActive) {
if (newActive == mActive) {
return;
}
// 當observer的狀態從active->inactive, 或者inactive->active的時候走如下流程
// owner
mActive = newActive;
boolean wasInactive = LiveData.this.mActiveCount == 0;
LiveData.this.mActiveCount += mActive ? 1 : -1;
if (wasInactive && mActive) {
onActive();
}
if (LiveData.this.mActiveCount == 0 && !mActive) {
onInactive();
}
//當observer是從inactive->active的時候,須要通知到觀察者
if (mActive) {
dispatchingValue(this);
}
}
}
複製代碼
@MainThread
public void observeForever(@NonNull Observer<T> observer) {
// 建立一個AlwaysActiveObserver對象
AlwaysActiveObserver wrapper = new AlwaysActiveObserver(observer);
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
if (existing != null && existing instanceof LiveData.LifecycleBoundObserver) {
throw new IllegalArgumentException("Cannot add the same observer"
+ " with different lifecycles");
}
if (existing != null) {
return;
}
//刷新數據
wrapper.activeStateChanged(true);
}
複製代碼