Skip to content

Instantly share code, notes, and snippets.

@cloud1105
Last active April 7, 2016 14:42
Show Gist options
  • Save cloud1105/79ab6a3be814a2148905d3a05f49178a to your computer and use it in GitHub Desktop.
Save cloud1105/79ab6a3be814a2148905d3a05f49178a to your computer and use it in GitHub Desktop.
RxJavaExample
//生成Observer
Observer<String> observer = new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
}
};
//生成Subscriber
//多了onStart和unsubscribe两个非常有用的方法
Subscriber<String> subscriber = new Subscriber<String>() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String o) {
}
};
if (subscriber.isUnsubscribed()) {
subscriber.unsubscribe();
}
//生成Observable的三种方法
//1.create
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("1");
subscriber.onNext("2");
subscriber.onNext("3");
subscriber.onCompleted();
}
});
//2.just
// 将会依次调用:
// onNext(1);
// onNext(2);
// onNext(3);
// onNext(4);
// onCompleted();
Observable<Integer> observable1 = Observable.just(1, 2, 3, 4);
//3.from 参数传入一个集合
// 将会依次调用:
// onNext(1);
// onNext(2);
// onNext(3);
// onNext(4);
// onCompleted();
Integer[] numbers = new Integer[]{1, 2, 3, 4};
Observable<Integer> observable2 = Observable.from(numbers);
observable.subscribe(observer);
Action1<String> onNextAction = new Action1<String>() {
// onNext()
@Override
public void call(String s) {
}
};
Action1<Throwable> onErrorAction = new Action1<Throwable>() {
// onError()
@Override
public void call(Throwable throwable) {
// Error handling
}
};
Action0 onCompletedAction = new Action0() {
// onCompleted()
@Override
public void call() {
}
};
// 自动创建 Subscriber ,并使用 onNextAction 来定义 onNext()
observable.subscribe(onNextAction);
// 自动创建 Subscriber ,并使用 onNextAction 和 onErrorAction 来定义 onNext() 和 onError()
observable.subscribe(onNextAction, onErrorAction);
// 自动创建 Subscriber ,并使用 onNextAction、 onErrorAction 和 onCompletedAction 来定义 onNext()、 onError() 和 onCompleted()
observable.subscribe(onNextAction, onErrorAction, onCompletedAction);
//1.打印字符数组
String[] names = {"leo","kris","hu","android"};
Observable.from(names)
.subscribe(new Action1<String>() {
@Override
public void call(String name) {
Log.d("leo", name);
}
});
//由 id 取得图片并显示
int drawableRes = R.drawable.default_image;
ImageView imageView = new ImageView(context);
Observable.create(new OnSubscribe<Drawable>() {
@Override
public void call(Subscriber<? super Drawable> subscriber) {
Drawable drawable = getTheme().getDrawable(drawableRes));
subscriber.onNext(drawable);
subscriber.onCompleted();
}
}).subscribe(new Observer<Drawable>() {
@Override
public void onNext(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Toast.makeText(activity, "Error!", Toast.LENGTH_SHORT).show();
}
});
int drawableRes = R.drawable.default_img;
ImageView imageView = ...;
Observable.create(new OnSubscribe<Drawable>() {
@Override
public void call(Subscriber<? super Drawable> subscriber) {
Drawable drawable = getTheme().getDrawable(drawableRes));
subscriber.onNext(drawable);
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
.observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
.subscribe(new Observer<Drawable>() {
@Override
public void onNext(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Toast.makeText(activity, "Error!", Toast.LENGTH_SHORT).show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment