LiveData LiveData是基于觀察者模式創建的,其中,LiveData是被觀察者,觀察者通過注冊方法,監聽被觀察者的資料變化,LiveData在資料發生變化的時候,會通知觀察者, LiveData是一個容器,存放資料的容器,它的資料變化可以被監聽,也就是LiveData是一個被觀察者,如下,創建了一個存放String的資料容器currentName:
public class NameViewModel extends ViewModel { // Create a LiveData with a String private MutableLiveData<String> currentName; public MutableLiveData<String> getCurrentName() { if (currentName == null) { currentName = new MutableLiveData<String>(); } return currentName; } // Rest of the ViewModel... }監聽LiveData資料變化,為LiveData添加觀察者,如下,添加一個nameObserver,監聽LiveData的資料變化,當LiveData的資料發生變化的的時候,onChanged方法會被回呼,從而更新UI,
public class NameActivity extends AppCompatActivity { private NameViewModel model; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Other code to setup the activity... // Get the ViewModel. model = new ViewModelProvider(this).get(NameViewModel.class); // Create the observer which updates the UI. final Observer<String> nameObserver = new Observer<String>() { @Override public void onChanged(@Nullable final String newName) { // Update the UI, in this case, a TextView. nameTextView.setText(newName); } }; // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer. model.getCurrentName().observe(this, nameObserver); } }更新LiveData資料的方式,使用setValue和postValue兩個方法 LiveData發布修改有setValue和postValue兩種方式,其中setValue只能在主執行緒呼叫,postValue則沒有這個限制
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String anotherName = "John Doe"; model.getCurrentName().setValue(anotherName); } });應用架構中的LiveData LiveData 具有生命周期感知能力,遵循 activity 和 fragment 等物體的生命周期,您可以使用 LiveData 在這些生命周期所有者和生命周期不同的其他物件(例如 ViewModel 物件)之間傳遞資料,ViewModel 的主要責任是加載和管理與界面相關的資料,因此非常適合作為用于保留 LiveData 物件的備選方法,您可以在 ViewModel 中創建 LiveData 物件,然后使用這些物件向界面層公開狀態, activity 和 fragment 不應保留 LiveData 實體,因為它們的用途是顯示資料,而不是保持狀態,此外,如果 activity 和 fragment 無需保留資料,還可以簡化單元測驗的撰寫, 擴展LiveData 如果觀察者的生命周期處于 STARTED 或 RESUMED 狀態,LiveData 會認為該觀察者處于活躍狀態,以下示例代碼說明了如何擴展 LiveData 類
public class StockLiveData extends LiveData<BigDecimal> { private StockManager stockManager; private SimplePriceListener listener = new SimplePriceListener() { @Override public void onPriceChanged(BigDecimal price) { setValue(price); } }; public StockLiveData(String symbol) { stockManager = new StockManager(symbol); } @Override protected void onActive() { stockManager.requestPriceUpdates(listener); } @Override protected void onInactive() { stockManager.removeUpdates(listener); } }當LiveData物件具有活躍觀察者時, 會呼叫 onActive() 方法,這意味著,您需要從此方法開始觀察股價更新, 當 LiveData 物件沒有任何活躍觀察者時,會呼叫 onInactive() 方法,由于沒有觀察者在監聽,因此沒有理由與 StockManager 服務保持連接, setValue(T) 方法將更新 LiveData 實體的值,并將更改告知活躍觀察者, LiveData使用總結
- 創建LiveData,使用viewModel類來包含
- 創建觀察者Observer
- 呼叫LiveData的observe方法將LiveData以及Observer建立起發布-訂閱關系
- 在適當的時機呼叫LiveData的setValue或者postValue發布新資料通知觀察者
- configuration changes時,不需要額外的處理來保存資料我們知道,當你把資料存盤在組件中時,當configuration change(比如語言、螢屏方向變化)時,組件會被recreate,然而系統并不能保證你的資料能夠被恢復的,當我們采用LiveData保存資料時,因為資料和組件分離了,當組件被recreate,資料還是存在LiveData中,并不會被銷毀,
public interface Observer<T> { /** * Called when the data is changed. * @param t The new data */ void onChanged(@Nullable T t); } // 注意,他是 abstract class public abstract class LiveData<T> { // 只有 onStart 后,對資料的修改才會觸發 observer.onChanged() public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {} // 無論何時,只要資料發生改變,就會觸發 observer.onChanged() public void observeForever(@NonNull Observer<T> observer) {} }
著作權宣告:作者:ttylinux 出處:http://www.cnblogs.com/ttylinux/ 本文著作權歸作者,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/556866.html
標籤:其他
下一篇:返回列表