// Get the ViewModel.
 model = ViewModelProviders.of(this).get(NameViewModel
 ::class.java)
// Create the observer which updates the UI.
 val nameObserver = Observer { newName ->
 // Update the UI, in this case, a TextView.
 nameTextView.text = newName
 }
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
 model.currentName.observe(this, nameObserver)
 }
 }
在讲nameObserver对象传给observe()方法后,存储在LiveData最近的值以参数的形式立即传递到onChange()方法中。当然,如果此时LiveData没有存储值的话,onChange()方法不会被调用。
更新 LiveData 对象
LiveData本身没有提供公共方法更新值。如果需要修改LiveData的数据的话,可以通过MutableLiveData来暴露共有方法setValue()和postValue()。通常在在ViewModel中使用MutableLiveData,而MutableLiveData暴露不可变的LiveData给Observer。与Observer建立关系后,通过修改LiveData的值从而更新Observer中的视图。
button.setOnClickListener {
 val anotherName = “GitCode”
 model.currentName.setValue(anotherName)
 }
当单击button时,字符串GitCode会存储到LiveData中,nameTextView的文本也会更新为GitCode。这里通过button的点击来给LiveData设置值,也可以网络或者本地数据库获取数据方式来设置值。
扩展 LiveData
可以通过下面的栗子来看看如何扩展LiveData。
class StockLiveData(symbol: String) : LiveData() {
 private val stockManager = StockManager(symbol)
private val listener = { price: BigDecimal ->
 value = price
 }
override fun onActive() {
 stockManager.requestPriceUpdates(listener)
 }
override fun onInactive() {
 stockManager.removeUpdates(listener)
 }
 }
首先建立一个StockLiveData并继承自LiveData,并重写两个重要方法。
- onActivite() 当有活跃状态的订阅者订阅LiveData时会回调该方法。意味着需要在这里监听数据的变化。
- onInactive() 当没有活跃状态的订阅者订阅LiveData时会回调该方法。此时没有必要保持StockManage服务象的连接。
- setValue() 注意到value=price这里是调用了setValue(price)方法,通过该方法更新LiveData的值,进而通知处于活跃状态的订阅者。
LiveData会认为订阅者的生命周期处于STARTED或RESUMED状态时,该订阅者是活跃的。
那么如何使用StockLiveData呢?
override fun onActivityCreated(savedInstanceState: Bundle?) {
 super.onActivityCreated(savedInstanceState)
 val myPriceListener: LiveData = …
 myPriceListener.observe(this, Observer { price: BigDecimal? ->
 // Update the UI.
 })
 }
以Fragment作LifecycleOwner的实例传递到observer()方法中,这样就将Observer绑定到拥有生命周期的拥有者。由于LiveData可以在多个Activity、Fragment和Service中使用,所以可以创建单例模式。
class StockLiveData(symbol: String) : LiveData() {
 private val stockManager: StockManager = StockManager(symbol)
private val listener = { price: BigDecimal ->
 value = price
 }
override fun onActive() {
 stockManager.requestPriceUpdates(listener)
 }
override fun onInactive() {
 stockManager.removeUpdates(listener)
 }
companion object {
 private lateinit var sInstance: StockLiveData
@MainThread
 fun get(symbol: String): StockLiveData {
 sInstance = if (::sInstance.isInitialized) sInstance else StockLiveData(symbol)
 return sInstance
 }
 }
 }
那么在Fragment可以这样使用:
class MyFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
 StockLiveData.get(symbol).observe(this, Observer { price: BigDecimal? ->
 // Update the UI.
 })
}
转换 LiveData
有时候在把数据分发给Observer前,转换存储在LiveData中的值,或者返回一个 基于已有值的LiveData对象 的另外一个LiveData对象。这时候就需要用到 Transformations类来处理了。
使用Transformations.map()方法可以改变其下游的结果:
 veData中的值,或者返回一个 基于已有值的LiveData对象 的另外一个LiveData对象。这时候就需要用到 Transformations类来处理了。
使用Transformations.map()方法可以改变其下游的结果:










