flow使用指南

138 阅读1分钟

flow流的使用

  • zip
headArray.asFlow().zip(headBgArray.asFlow()) { it1, it2 ->
    var index = headArray.indexOf(it1)
    var type = when (index) {
        0 -> HomeTabType.tab_news
        1 -> HomeTabType.tab_video
        2 -> HomeTabType.tab_headline
        else -> HomeTabType.tab_headline
    }

    CompetitionBean().apply {
        this.name = getString(it1)
        this.defalutHeadBg = it2
        this.type = type
    }
}.collect {
    list.add(it)
}

输出headArray与HeadBgArray的转换,最终通过collect进行数据整合输出。

  • flatmapConcat
headArray.asFlow().flatMapConcat {
    flow<CompetitionBean> {
                  emit(CompetitionBean().apply {
                      this.name = getString(it1)
                      this.defalutHeadBg = headBgArray[it2]
                  }
                      )
                  }
              }.collect {
                  list.add(it)
              }

输出headArray转换后的集合。