pageSize = pageSize, fetchExecutor = executor ) val loadStatus = Transformations.switchMap...newsDataSourceFactory.dataSourceLiveData) { it.loadStatus } val initStatus = Transformations.switchMap...NewsListingModel>() val adapter = NewsAdapter { retry() } val newsLoadStatus = Transformations.switchMap...(newsListing) { it.loadStatus } val refreshLoadStatus = Transformations.switchMap(newsListing...) { it.refreshStatus } val articleList = Transformations.switchMap(newsListing) {
;LiveData user = Transformations.switchMap(userId, id -> getUser(id) ); 使用这些转换允许在整个调用链中携带观察者的 Lifecycle...String> addressInput = new MutableLiveData(); public final LiveData postalCode = Transformations.switchMap
转化LiveData时需要用到Transformations.map()和Transformations.switchMap()等方法。...= Transformations.map(userLiveData, user -> { user.name + " " + user.lastName }); Transformations.switchMap...() 使用Transformations.switchMap()方法同样可以改变LiveData下游的结果,但传递给switchMap()函数的必须是一个LiveData对象,如下所示。...getUser(String id) { ...; } LiveData userId = ...; LiveData user = Transformations.switchMap...addressInput = new MutableLiveData(); public final LiveData postalCode = Transformations.switchMap
Popularity.NORMAL } } } enum class Popularity { NORMAL, // 普通 POPULAR, // 受欢迎 STAR // 巨星 } Transformations.switchMap...private fun getUser(id: String): LiveData { ... } val userId: LiveData = ... val user = Transformations.switchMap
String> userName = Transformations.map(userLiveData, user -> { user.name + " " + user.lastName }); Transformations.switchMap...private LiveData getUser(String id) { ...; } LiveData userId = ...; LiveData user = Transformations.switchMap
Transformations.map Transformations.switchMap 请注意,当你的View被销毁时,你不需要销毁这些订阅,因为View的lifecycle会被传播到下游后继续订阅...val repositoryResult = Transformations.switchMap(userManager.userID) { userID -> repository.getDataForUser...(Spoiler: this is what Transformations.switchMap does! See solution below.)...private val newNumberEvent = MutableLiveData>() val randomNumber: LiveData = Transformations.switchMap
String> userName = Transformations.map(userLiveData, user -> { user.name + " " + user.lastName }); 使用Transformations.switchMap...private fun getUser(id: String): LiveData { ... } val userId: LiveData = ... val user = Transformations.switchMap
String> userName = Transformations.map(userLiveData, user -> { 3 user.name + " " + user.lastName 4}); Transformations.switchMap...LiveData getUser(String id) { 2 ...; 3} 4LiveData userId = ...; 5LiveData user = Transformations.switchMap
String> userName = Transformations.map(userLiveData, user -> { user.name + " " + user.lastName }); Transformations.switchMap...LiveData getUser(String id) { ...; } LiveData userId = ...; LiveData user = Transformations.switchMap...String> addressInput = new MutableLiveData(); public final LiveData postalCode = Transformations.switchMap
21998/com.hfy.androidlearning I/Lifecycle_Test: onChanged1: 100 + Transformations.map 2.3.2 数据切换 - Transformations.switchMap...如果想要根据某个值 切换观察不同LiveData数据,则可以使用Transformations.switchMap()方法。...//liveDataSwitchMap由switchMap()方法生成,用于添加观察者 LiveData liveDataSwitchMap = Transformations.switchMap...Transformations.switchMap()就用于实现这一逻辑,返回值liveDataSwitchMap添加观察者就可以了。
size } Transformations.switchMap() 相当于对上面的做了个判断,根据不同的需求返回不同的 LiveData private val _state by lazy {...MutableLiveData() } var tranLiveData = Transformations.switchMap(_state) { if (it.list
这里监听了 user 的变化,当 user 更新之后去 BookRepo 中获取图书信息,并返回一个 LiveData val uiModel: LiveData = Transformations.switchMap...uiModel 取消监听之后,这里 ViewModel 和 UserRepo/BookRepo 之间监听也会断开 val uiModel: LiveData = Transformations.switchMap
Transformations.switchMap 允许你创建响应其他 LiveData 实例的改变的 LiveData ,它还允许在调用链上传递观察者的生命周期信息: LiveData repo...= Transformations.switchMap(repoIdLiveData, repoId -> { if (repoId.isEmpty()) {
val liveData: LiveData = Transformations.switchMap(sourceliveData) { liveData(viewModelScope.coroutineContext
Transformations.switchMap让你创建一个新的LiveData,对其他LiveData实例的变化做出反应。它还允许在整个链条上携带观察者的生命周期信息。...LiveData repo = Transformations.switchMap(repoIdLiveData, repoId -> { if (repoId.isEmpty
registerLiveData = MutableLiveData() //对于liveData进行转换 val verifyStatusLiveData = Transformations.switchMap...verifyLiveData) { query -> Repository.getVerifyStatus(query) } val registerStatusLiveData = Transformations.switchMap
livePageList = LivePagedListBuilder(sourceFactory, config).build() val refreshState = Transformations.switchMap...it.initialLoad } return Listing( pagedList = livePageList, networkState = Transformations.switchMap
MutableLiveData userIdLiveData = new MutableLiveData(); final LiveData userLiveData = Transformations.switchMap...Long): LiveData { //... } //... val userIdLiveData = MutableLiveData() val userLiveData = Transformations.switchMap
val searchResults: LiveData> = Transformations.switchMap(searchQuery) { getSearchResults
领取专属 10元无门槛券
手把手带您无忧上云