本教學基於RxJava1.x版本進行全面講解,後續課程將陸續更新,敬請關注…
private ArrayList<Student> initPersons() { ArrayList<Student> persons = new ArrayList<>(); persons.add(new Student("张三", 16)); persons.add(new Student("李四", 17)); persons.add(new Student("王二麻子", 18)); persons.add(new Student("张三", 22)); return persons; }
Observable .from(initPersons()) //过滤重复名字的项 .distinct(new Func1<Student, String>() { @Override public String call(Student student) { return student.name; } }) .subscribe(new Action1<Student>() { @Override public void call(Student student) { Log.i(TAG, "call: "+student); } });
IT520: call: Student{name='张三', age=16} IT520: call: Student{name='李四', age=17} IT520: call: Student{name='王二麻子', age=18}
Observable.just(1, 2, 3, 4, 5) .filter(new Func1<Integer, Boolean>() { @Override public Boolean call(Integer item) { return( item < 4 ); } }) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Next: 1 Next: 2 Next: 3 Sequence complete.
Observable.just(1, 2, 3) .first() .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Next: 1 Sequence complete.
類似的函數還有firstOrDefault(),該函數在佇列發送的過程如果隊列為null,也就是找不到第一項,則會傳送一個預設值。
4.Last
呼叫了該函數可以讓傳送的資料佇列只傳送最後一項。 範例程式碼Observable.just(1, 2, 3) .last() .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Next: 3 Sequence complete.
類似的函數還有lastOrDefault(T)
5.ElementAt
#ElementAt操作符取得原始Observable發射的資料序列指定索引位置的資料項,然後當做自己的唯一資料發射。 如果你傳遞的是負數,或是原始Observable的資料項數小於index+1,將會拋出一個IndexOutOfBoundsException例外。 範例程式碼如下:Observable.just(1, 2, 3, 4, 1, 4) .elementAt(3) .subscribe(new Action1<Integer>() { @Override public void call(Integer value) { Log.i(TAG, "call: " + value); } });
com.m520it.rxjava I/IT520: call: 4
Observable.just(1, 2, 3, 4, 5, 6, 7, 8) .take(4) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Next: 1 Next: 2 Next: 3 Next: 4 Sequence complete.
Observable.just(1, 2, 3, 4, 5, 6, 7, 8) .takeLast(4) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Next: 5 Next: 6 Next: 7 Next: 8 Sequence complete.
Observable .interval(1000, TimeUnit.MILLISECONDS)//每秒发送1个数字 .sample(2000,TimeUnit.MILLISECONDS)//每2秒采样一次 .subscribe(new Action1<Long>() { @Override public void call(Long aLong) { Log.i(TAG, "call: "+aLong); } });
Observable.just(1, 2, 3, 4, 1) .ignoreElements()//不发送任何信息 直接发送onCompleted() .subscribe(new Subscriber<Integer>() { @Override public void onCompleted() { Log.i(TAG, "onCompleted: "); } @Override public void onError(Throwable e) { } @Override public void onNext(Integer integer) { Log.i(TAG, "onNext: "+integer); } });