在某些情况下,我想更改LiveData的值,并且不希望观察者通知更改。我该怎么办?
答案 0 :(得分:2)
我只使用Pair<T, Boolean>
作为值类型,观察者可以检查pair.second
这个想法很简单。将您的MutableLiveData<String>
替换为MutableLiveData<Pair<String, Boolean>>
当您想用值s
通知观察者时,只需调用liveData.setValue(new Pair(s, true))
,然后调用liveData.setValue(new Pair(s, false))
在观察者侧,只需检查pair.second
即可区分这两种情况
编辑:我正在用电话回答问题,看来stackoverflow隐藏了尖括号包围的字符。
答案 1 :(得分:1)
我认为我的自定义课程会为您提供帮助
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.util.Pair;
import java.util.ArrayList;
import java.util.List;
public class SSBLiveData<T> extends MutableLiveData<T> {
private List<Pair<LifecycleOwner, Observer>> observers = new ArrayList<>();
@MainThread
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer observer) {
observers.add(new Pair<>(owner, observer));
super.observe(owner, observer);
}
@MainThread
private void resetAllObservers() {
for (Pair<LifecycleOwner, Observer> observer : observers) {
super.observe(observer.first, observer.second);
}
}
@MainThread
private void removeAllObservers() {
for (Pair<LifecycleOwner, Observer> observer : observers) {
removeObservers(observer.first);
}
}
@MainThread
void setValueWithoutNotify(T value) {
removeAllObservers();
super.setValue(value);
System.out.println("Not notifying value " + getValue());
}
// For androidx use this method
/*@MainThread
@Override
public void removeObserver(@NonNull Observer<? super T> observer) {
for (Pair<LifecycleOwner, Observer> observerItem : observers) {
if (observerItem.second.equals(observer) && observerItem.first.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
observers.remove(observerItem);
}
}
super.removeObserver(observer);
}*/
@MainThread
@Override
public void removeObserver(@NonNull Observer<T> observer) {
for (Pair<LifecycleOwner, Observer> observerItem : observers) {
if (observerItem.second.equals(observer) && observerItem.first.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
observers.remove(observerItem);
}
}
super.removeObserver(observer);
}
@Override
public void setValue(T value) {
super.setValue(value);
if (!hasObservers()) {
resetAllObservers();
}
}
}
以这种方式使用
SSBLiveData<String> liveData = new SSBLiveData<>();
liveData.setValueWithoutNotify("It won't get notified");
liveData.setValue("It will get notified");
答案 2 :(得分:1)
已接受答案中描述的同一类,但在Kotlin中被覆盖,并使用 AndroidX 导入
import android.util.Pair
import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.*
class ExtendedLiveData<T>() : MutableLiveData<T>() {
private val observers: MutableList<Pair<LifecycleOwner, Observer<in T>>> = ArrayList()
@MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
observers.add(Pair(owner, observer))
super.observe(owner, observer)
}
@MainThread
private fun resetAllObservers() {
for (observer in observers) {
super.observe(observer.first, observer.second)
}
}
@MainThread
private fun removeAllObservers() {
for (observer in observers) {
removeObservers(observer.first)
}
}
@MainThread
fun setValueWithoutNotify(value: T) {
removeAllObservers()
super.setValue(value)
println("Not notifying value " + getValue())
}
@MainThread
override fun removeObserver(observer: Observer<in T>) {
for (observerItem in observers) {
if (observerItem.second == observer && observerItem.first.lifecycle
.currentState == Lifecycle.State.DESTROYED
) {
observers.remove(observerItem)
}
}
super.removeObserver(observer)
}
override fun setValue(value: T) {
super.setValue(value)
if (!hasObservers()) {
resetAllObservers()
}
}
fun resetValue() {
super.setValue(null)
}
}