阅读Google docs我发现(有点)使用selectedItem
将一个被触发的事件传播给其他观察者的例子,这是我当前的实现:
视图模型
public void onListItemClicked(Item item) {
if (selectedItem.getValue() == item) {
return;
}
selectedItem.postValue(item);
}
public LiveData<Item> getSelectedItem() {
if (selectedItem == null) {
selectedItem = new MutableLiveData<>();
}
return selectedItem;
}
视图
ListViewModel viewModel = ViewModelProviders.of(this).get(ListViewModel.class);
viewModel.getSelectedItem().observe(this, new Observer<Item>() {
@Override
public void onChanged(@Nullable Item item) {
if (item != null) {
openDetailActivity(item);
}
}
});
当用户点击列表时:
@Override
public void onItemClicked(Item item) {
viewModel.onListItemClicked(item);
}
所有好的和所有的工作,问题是当用户旋转屏幕并重新创建ListActivity
检测到更改并将在订阅时打开DetailActivity
。
我找到了一个解决方法,在selectedItem.postValue(null);
上添加getSelectedItem()
,但它有点hacky。
Ofc有人可能会争辩说,开放细节活动和宣传甚至应该是分开的,但我想知道是否有人有更好的实施/建议。
编辑
使用SingleLiveEvent
是要走的路。这可以确保你的ViewModel
只发射一次事件。
这是参考文章:
我用Kotlin
课程创造了一个要点。我一直在成功使用这些用例:
我会保持最新的要点,但我也会将代码保留在这里(仅供参考,因为我每次更改要点时都不会编辑此答案):
package YOUR_PACKAGE
import androidx.annotation.MainThread
import androidx.annotation.Nullable
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicBoolean
/**
* A lifecycle-aware observable that sends only new updates after subscription, used for events like
* navigation and Snackbar messages.
* <p>
* This avoids a common problem with events: on configuration change (like rotation) an update
* can be emitted if the observer is active. This LiveData only calls the observable if there's an
* explicit call to setValue() or call().
* <p>
* Note that only one observer is going to be notified of changes.
*/
class SingleLiveEvent<T> : MutableLiveData<T>() {
private val mPending = AtomicBoolean(false)
@MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
// Observe the internal MutableLiveData
super.observe(owner, Observer<T> { t ->
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t)
}
})
}
@MainThread
override fun setValue(@Nullable t: T?) {
mPending.set(true)
super.setValue(t)
}
/**
* Used for cases where T is Void, to make calls cleaner.
*/
@MainThread
fun call() {
value = null
}
}
老答案:
经过相当多的研究并与谷歌开发者取得联系。建议的解决方案是分别负责。打开活动应该响应click事件而不是实际更改,这种类型的selectedItem
场景对于与其他侦听视图的分离通信特别有用。例如,同一活动中的另一个片段