Skip to content

Commit d55fda7

Browse files
committed
add demo
1 parent f352235 commit d55fda7

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ dependencies {
2626
})
2727
compile 'com.android.support:appcompat-v7:25.3.1'
2828
compile 'com.android.support.constraint:constraint-layout:1.0.2'
29-
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
30-
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
29+
compile 'io.reactivex:rxandroid:1.2.0'
30+
compile 'io.reactivex:rxjava:1.1.5'
31+
// compile 'io.reactivex.rxjava2:rxjava:2.0.1'
32+
// compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
3133
testCompile 'junit:junit:4.12'
3234
}

app/src/main/java/scut/carson_ho/rxjava_operators/MainActivity.java

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5-
import android.util.Log;
65

7-
import io.reactivex.Observable;
8-
import io.reactivex.ObservableEmitter;
9-
import io.reactivex.ObservableOnSubscribe;
10-
import io.reactivex.Observer;
11-
import io.reactivex.disposables.Disposable;
126

137
public class MainActivity extends AppCompatActivity {
148

@@ -19,44 +13,72 @@ protected void onCreate(Bundle savedInstanceState) {
1913
setContentView(R.layout.activity_main);
2014

2115

22-
// 1. 通过creat()创建被观察者对象
23-
Observable.create(new ObservableOnSubscribe<Integer>() {
24-
// 2. 在复写的subscribe()里定义需要发送的事件
25-
@Override
26-
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
27-
emitter.onNext(1);
28-
emitter.onNext(2);
29-
emitter.onNext(3);
30-
emitter.onNext(4);
16+
// Observable.create(new Observable.OnSubscribe<Integer>() {
17+
// @Override
18+
// public void call(Subscriber<? super Integer> subscriber) {
19+
// // 不发送任何有效事件
20+
// // subscriber.onNext(1);
21+
// // subscriber.onNext(2);
22+
// // subscriber.onNext(3);
23+
//
24+
// subscriber.onCompleted();
25+
// // 仅发送Complete事件
26+
// }
27+
// }).defaultIfEmpty(10) // 若仅发送了Complete事件,默认发送 值 = 10
28+
// .subscribe(new Subscriber<Integer>() {
29+
// @Override
30+
// public void onCompleted() {
31+
// Log.v(TAG, "onCompleted");
32+
// }
33+
// @Override
34+
// public void onError(Throwable e) {
35+
// Log.v(TAG, "onError:"+e.getMessage());
36+
// }
37+
// @Override
38+
// public void onNext(Integer integer) {
39+
// Log.v(TAG, "onNext:"+integer);
40+
// }
41+
// });
3142

32-
emitter.onComplete();
33-
} // 至此,一个被观察者对象(Observable)就创建完毕
34-
}).subscribe(new Observer<Integer>() {
35-
// 以下步骤仅为展示一个完整demo,可以忽略
36-
// 3. 通过通过订阅(subscribe)连接观察者和被观察者
37-
// 4. 创建观察者 & 定义响应事件的行为
38-
@Override
39-
public void onSubscribe(Disposable d) {
40-
Log.d(TAG, "开始采用subscribe连接");
41-
}
42-
// 默认最先调用复写的 onSubscribe()
43-
44-
@Override
45-
public void onNext(Integer value) {
46-
Log.d(TAG, "接收到了事件"+ value );
47-
}
48-
49-
@Override
50-
public void onError(Throwable e) {
51-
Log.d(TAG, "对Error事件作出响应");
52-
}
53-
54-
@Override
55-
public void onComplete() {
56-
Log.d(TAG, "对Complete事件作出响应");
57-
}
58-
59-
});
43+
//
44+
//// 1. 通过creat()创建被观察者对象
45+
// Observable.create(new ObservableOnSubscribe<Integer>() {
46+
// // 2. 在复写的subscribe()里定义需要发送的事件
47+
// @Override
48+
// public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
49+
// emitter.onNext(1);
50+
// emitter.onNext(2);
51+
// emitter.onNext(3);
52+
// emitter.onNext(4);
53+
//
54+
// emitter.onComplete();
55+
// } // 至此,一个被观察者对象(Observable)就创建完毕
56+
// }).subscribe(new Observer<Integer>() {
57+
// // 以下步骤仅为展示一个完整demo,可以忽略
58+
// // 3. 通过通过订阅(subscribe)连接观察者和被观察者
59+
// // 4. 创建观察者 & 定义响应事件的行为
60+
// @Override
61+
// public void onSubscribe(Disposable d) {
62+
// Log.d(TAG, "开始采用subscribe连接");
63+
// }
64+
// // 默认最先调用复写的 onSubscribe()
65+
//
66+
// @Override
67+
// public void onNext(Integer value) {
68+
// Log.d(TAG, "接收到了事件"+ value );
69+
// }
70+
//
71+
// @Override
72+
// public void onError(Throwable e) {
73+
// Log.d(TAG, "对Error事件作出响应");
74+
// }
75+
//
76+
// @Override
77+
// public void onComplete() {
78+
// Log.d(TAG, "对Complete事件作出响应");
79+
// }
80+
//
81+
// });
6082
}
6183

6284
}

0 commit comments

Comments
 (0)