这是我的实现:
val just1 = ArrayList<SomeClass1>()
填充一些项目
fun populateJust1() { just1.add(SomeClass1("23",23)) just1.add(SomeClass1("24",24)) just1.add(SomeClass1("25",25)) } populateJust1()
和区间Observable压缩
Observable.fromIterable(just1) .zipWith(Observable.interval(2,TimeUnit.SECONDS)) { item: SomeClass1,interval: Long -> item } .subscribe(Consumer<SomeClass1> { someClass1 -> Log.v("someClass1",someClass1.toString()) })
但是,IDE,Android Studio 3.0以红色方式强调了zipWith运算符,并说:
None of the following functions can be called with the arguments supplied.
zipWith(((observer: Observer) → Unit)!,((t1: SomeClass1,t2: Long) → R)!) where R cannot be inferred; U = Long for fun zipWith(other: ((observer: Observer) → Unit)!,zipper: ((t1: SomeClass1,t2: U) → R)!): Observable! defined in io.reactivex.Observable
zipWith(ObservableSource!,BiFunction!) where R cannot be inferred; U = Long! for fun zipWith(other: ObservableSource!,zipper: BiFunction!): Observable! defined in io.reactivex.Observable
zipWith((Mutable)Iterable!,BiFunction!) where U,R cannot be inferred for fun zipWith(other: (Mutable)Iterable!,t2: Long) → R)!) where R cannot be inferred; U = Long for fun zipWith(other: (Mutable)Iterable!,t2: U) → R)!): Observable! defined in io.reactivex.Observable
怎么了?我刚从Java类转换了这个代码,其中一切都是正确的
解决方法
使用BiFunction而不是lambda:
data class SomeClass(val a: String,val b: Int) val list = listOf(SomeClass("1",1),SomeClass("2",2)) Observable .fromIterable(list) .zipWith(Observable.interval(2,TimeUnit.SECONDS),BiFunction { item: SomeClass,_: Long -> item }) .subscribe { Log.v("someClass",it.toString())