对Android的RxJava用法的初步整理。针对事件序列的处理和再发送
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.2'
1.map (数据类型转换 如:int -> String)
2.flatMap 重新返回一个 Observable.fromArray(arry)
1.map (数据类型转换 如:int -> String)
package com.archie.arrangeandroid;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Integer[] integers = {1, 2, 3, 4, 5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Observable.fromArray(integers)
.map(new Function<Integer, String>() {
/*map 数据类型转换 如:int -> String*/
@Override
public String apply(@NonNull Integer s) throws Exception {
Log.i(TAG, "apply: " + s);
return "map: " + s;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.i(TAG, "onSubscribe: " + "绑定前的操作");
}
@Override
public void onNext(@NonNull String student) {
Log.i(TAG, "onNext: " + student);
}
@Override
public void onError(@NonNull Throwable e) {
Log.i(TAG, "onError: " + "出现错误");
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete: " + "已结束");
}
});
}
}
1) 输出如下
3A(ZXSM2RXQIFF0A_CP16WE.png
2. flatMap 重新返回一个 Observable.fromArray(arry)
需要二次请求数据的时候可用到
package com.archie.arrangeandroid;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
class Student {
String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private static final String TAG = "MainActivity";
String[] strings = {"one", "two", "three", "four", "five"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Observable.fromArray(strings)
.flatMap(new Function<String, ObservableSource<Student>>() {
/*flatMap 重新返回一个Observable*/
@Override
public ObservableSource<Student> apply(@NonNull String s) throws Exception {
Student student = new Student(s);
return Observable.fromArray(student);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Student>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.i(TAG, "onSubscribe: " + "绑定前的操作");
}
@Override
public void onNext(@NonNull Student student) {
Log.i(TAG, "onNext: " + student.getName());
}
@Override
public void onError(@NonNull Throwable e) {
Log.i(TAG, "onError: " + "出现错误");
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete: " + "已结束");
}
});
}
}
1) 输出如下
0.png
网友评论