Android 如何转换LiveData<;列表<;Foo>&燃气轮机;进入LiveData<;列表<;酒吧>>;?

Android 如何转换LiveData<;列表<;Foo>&燃气轮机;进入LiveData<;列表<;酒吧>>;?,android,android-livedata,Android,Android Livedata,假设我们有两个LiveData对象: LiveData<List<Foo>> fooList; LiveData<List<Bar>> barList; LiveData傻瓜; livedatabarlist; 通过某种方法(或构造函数),Foo可以转换为Bar对象。 将具有Foo对象列表的第一个可观察对象转换为具有Bar对象列表的可观察对象的最佳方式是什么 我知道有可能做到这一点: barList = Transformations.map(

假设我们有两个LiveData对象:

LiveData<List<Foo>> fooList;
LiveData<List<Bar>> barList;
LiveData傻瓜;
livedatabarlist;
通过某种方法(或构造函数),Foo可以转换为Bar对象。 将具有Foo对象列表的第一个可观察对象转换为具有Bar对象列表的可观察对象的最佳方式是什么

我知道有可能做到这一点:

barList = Transformations.map(fooList, fooList1 -> {
        List<Bar> barList = new ArrayList<>();
        for (Foo foo: fooList1) {
            barList.add(new Bar(foo)); 
        }
        return barList;
    });
barList=Transformations.map(傻瓜列表,傻瓜列表1->{
List barList=新建ArrayList();
for(Foo-Foo:傻瓜1){
添加(新酒吧(foo));
}
返回条码列表;
});

但是,难道没有一种更好的方法类似于RxJava中的flatMap操作符吗?通过这种方法,我们可以动态地对列表中的项目进行所有必要的转换,而不是像上面的示例那样处理列表本身吗?

您可以创建自己的转换,按照源代码中显示的配方进行转换。FWIW演示了一个自定义的
filter()
实现:

/***
 Copyright (c) 2017 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 Covered in detail in the book _Android's Architecture Components_
 https://commonsware.com/AndroidArch
 */

package com.commonsware.android.livedata;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

class LiveTransmogrifiers {
  interface Confirmer<T> {
    boolean test(T thingy);
  }

  @MainThread
  static <X> LiveData<X> filter(@NonNull LiveData<X> source,
                                @NonNull final Confirmer<X> confirmer) {
    final MediatorLiveData<X> result=new MediatorLiveData<>();

    result.addSource(source, new Observer<X>() {
      @Override
      public void onChanged(@Nullable X x) {
        if (confirmer.test(x)) {
          result.setValue(x);
        }
      }
    });

    return(result);
  }
}
/***
版权所有(c)2017 Commonware,LLC
根据Apache许可证2.0版(以下简称“许可证”)获得许可;你不可以
除非符合许可证,否则请使用此文件。你可以得到一份
许可证的有效期在http://www.apache.org/licenses/LICENSE-2.0. 除非需要
根据适用法律或书面同意,根据
许可证按“原样”分发,无任何保证或条件
任何形式的,无论是明示的还是暗示的。请参阅许可证以了解具体的信息
管理许可证下的权限和限制的语言。
本书详细介绍了Android的体系结构组件_
https://commonsware.com/AndroidArch
*/
包com.commonware.android.livedata;
导入android.arch.lifecycle.LiveData;
导入android.arch.lifecycle.MediatorLiveData;
导入android.arch.lifecycle.Observer;
导入android.support.annotation.MainThread;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
类活变形金刚{
接口确认器{
布尔检验(T-thingy);
}
@主线
静态LiveData筛选器(@NonNull LiveData source,
@非空最终确认人(最终确认人){
最终MediatorLiveData结果=新MediatorLiveData();
result.addSource(source,new Observer()){
@凌驾
更改后的公共void(@Nullable X X){
if(确认测试(x)){
结果:设定值(x);
}
}
});
返回(结果);
}
}

否则,没有。
LiveData
意味着它是一种轻量级的RxJava模拟物,具有生命周期意识。如果您需要大量运算符,最好直接使用RxJava。

通过您的示例,我仍然不明白它如何有助于处理列表的元素而不是整个列表本身。也就是说,在您提到的代码中,在OnChanged函数中,对于我的例子,将有元素列表,而不是只有一个元素,并且我必须执行与上面Transformations.map代码中相同的操作。@AlexeyTimokhin:您需要查看RxJava如何实现
flatMap()
。然后,看看是否可以创建等效的
LiveData
转换。也许这是不可能的。在这种情况下,我的答案的最后一段仍然有效。@commonware为什么不切换地图?今天我会在你的聊天时间来和你聊天site@trocchietto:我写答案时可能忘记了
switchMap()
存在。我不是100%肯定它会涵盖OP的场景,但可能会。Ok会在今天下午4点问你。谢谢