├── .idea
├── .name
├── dictionaries
│ └── Administrator.xml
├── encodings.xml
├── vcs.xml
├── copyright
│ └── profiles_settings.xml
├── modules.xml
├── runConfigurations.xml
├── compiler.xml
├── gradle.xml
├── misc.xml
└── findbugs-idea.xml
├── app
├── .gitignore
├── libs
│ ├── rxjava-1.3.0.jar
│ └── rxandroid-1.2.1.aar
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ ├── menu
│ │ │ │ ├── menu_main.xml
│ │ │ │ ├── menu_base.xml
│ │ │ │ ├── menu_fitst.xml
│ │ │ │ ├── menu_sample.xml
│ │ │ │ ├── menu_scan.xml
│ │ │ │ ├── menu_buffer.xml
│ │ │ │ ├── menu_debounce.xml
│ │ │ │ ├── menu_distinct.xml
│ │ │ │ ├── menu_flat_map.xml
│ │ │ │ ├── menu_from.xml
│ │ │ │ ├── menu_groupby.xml
│ │ │ │ ├── menu_map.xml
│ │ │ │ ├── menu_window.xml
│ │ │ │ ├── menu_defer.xml
│ │ │ │ ├── menu_interval.xml
│ │ │ │ ├── menu_skip_and_take.xml
│ │ │ │ ├── menu_create.xml
│ │ │ │ ├── menu_repeat.xml
│ │ │ │ └── menu_element_at_and_first.xml
│ │ │ └── layout
│ │ │ │ ├── activity_publish_and_connect.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── activity_fitst.xml
│ │ │ │ ├── activity_scan.xml
│ │ │ │ ├── activity_buffer.xml
│ │ │ │ ├── activity_debounce.xml
│ │ │ │ ├── activity_distinct.xml
│ │ │ │ ├── activity_from.xml
│ │ │ │ ├── activity_map.xml
│ │ │ │ ├── activity_sample.xml
│ │ │ │ ├── activity_window.xml
│ │ │ │ ├── activity_flat_map.xml
│ │ │ │ ├── activity_groupby.xml
│ │ │ │ ├── create.xml
│ │ │ │ ├── activity_defer.xml
│ │ │ │ ├── activity_interval.xml
│ │ │ │ ├── activity_skip_and_take.xml
│ │ │ │ ├── activity_repeat.xml
│ │ │ │ ├── activity_element_at_and_first.xml
│ │ │ │ └── activity_base.xml
│ │ └── java
│ │ │ └── cn
│ │ │ └── com
│ │ │ └── chaoba
│ │ │ └── rxjavademo
│ │ │ ├── BaseActivity.java
│ │ │ ├── connectable
│ │ │ ├── RefCountActivity.java
│ │ │ ├── PublishAndConnectActivity.java
│ │ │ └── ReplayActivity.java
│ │ │ ├── transforming
│ │ │ ├── ScanActivity.java
│ │ │ ├── BufferActivity.java
│ │ │ ├── MapAndCastActivity.java
│ │ │ ├── WindowActivity.java
│ │ │ ├── FlatMapActivity.java
│ │ │ └── GroupbyActivity.java
│ │ │ ├── conditional_boolean
│ │ │ ├── SequenceEqualActivity.java
│ │ │ ├── SkipUntilSkipWhileActivity.java
│ │ │ ├── DefaultIfEmptyActivity.java
│ │ │ ├── TakeUntilTakeWhileActivity.java
│ │ │ ├── ContainsActivity.java
│ │ │ └── AllAndAmbActivity.java
│ │ │ ├── utility
│ │ │ ├── MaterializeActivity.java
│ │ │ ├── TimeIntervalTimeStampActivity.java
│ │ │ ├── DelayActivity.java
│ │ │ ├── TimeoutActivity.java
│ │ │ ├── UsingActivity.java
│ │ │ └── DoActivity.java
│ │ │ ├── aggregate
│ │ │ ├── ConcatAndCountActivity.java
│ │ │ └── Reducectivity.java
│ │ │ ├── creatingobserver
│ │ │ ├── IntervalActivity.java
│ │ │ ├── FromActivity.java
│ │ │ ├── RepeatAndTimerActivity.java
│ │ │ ├── DeferAndJustActivity.java
│ │ │ ├── CompletableActivity.java
│ │ │ ├── SingleActivity.java
│ │ │ └── CreateAndRangeActivity.java
│ │ │ ├── Filtering
│ │ │ ├── SkipAndTakeActivity.java
│ │ │ ├── DistinctActivity.java
│ │ │ ├── SampleActivity.java
│ │ │ ├── ElementAtAndFilterActivity.java
│ │ │ ├── FirstActivity.java
│ │ │ └── DebounceActivity.java
│ │ │ ├── scheduler
│ │ │ ├── FromExecutorActivity.java
│ │ │ └── SubscribeOnAndObserveOnActivity.java
│ │ │ ├── combining
│ │ │ ├── StartWithAndSwitchActivity.java
│ │ │ ├── ZipActivity.java
│ │ │ ├── CombineLatestActivity.java
│ │ │ ├── MergeActivity.java
│ │ │ └── JoinActivity.java
│ │ │ ├── errorhandling
│ │ │ ├── OnExceptionActivity.java
│ │ │ ├── OnErrorActivity.java
│ │ │ └── RetryActivity.java
│ │ │ ├── custom
│ │ │ └── CustomActivity.java
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── cn
│ │ │ └── com
│ │ │ └── chaoba
│ │ │ └── rxjavademo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── cn
│ │ └── com
│ │ └── chaoba
│ │ └── rxjavademo
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── README.md
├── gradle.properties
├── gradlew.bat
├── gradlew
└── RxJavaDemo.iml
/.idea/.name:
--------------------------------------------------------------------------------
1 | RxJavaDemo
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/libs/rxjava-1.3.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/app/libs/rxjava-1.3.0.jar
--------------------------------------------------------------------------------
/app/libs/rxandroid-1.2.1.aar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/app/libs/rxandroid-1.2.1.aar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/dictionaries/Administrator.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | .DS_Store
6 | /build
7 | /captures
8 | /.idea
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Chaoba/RxJavaDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # RxJavaDemo
2 |
3 | Demos for the useage of Rxjava operators in android app.
4 |
5 | Source for [my blogs](http://mushuichuan.com/tags/RxJava/) about Rxjava operators.
6 |
7 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Jul 08 16:17:53 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 180dp
6 | 16dp
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/test/java/cn/com/chaoba/rxjavademo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/cn/com/chaoba/rxjavademo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_base.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_fitst.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_sample.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_scan.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_buffer.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_debounce.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_distinct.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_flat_map.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_from.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_groupby.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_map.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_window.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_defer.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_interval.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_skip_and_take.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_create.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_repeat.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_element_at_and_first.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_publish_and_connect.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\software\adt-bundle-windows-x86-20130917\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_fitst.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_scan.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_buffer.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_debounce.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_distinct.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_from.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_map.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_sample.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_window.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_flat_map.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_groupby.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/create.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_defer.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_interval.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_skip_and_take.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_repeat.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_element_at_and_first.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Wed Sep 30 10:10:00 CST 2015
16 | systemProp.https.proxyPort=8580
17 | systemProp.http.proxyHost=127.0.0.1
18 | systemProp.https.proxyHost=127.0.0.1
19 | systemProp.http.proxyPort=8580
20 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 | import android.widget.Button;
7 | import android.widget.TextView;
8 |
9 | import rx.Observable;
10 | import rx.android.schedulers.AndroidSchedulers;
11 |
12 | public class BaseActivity extends AppCompatActivity {
13 |
14 | protected Button mLButton, mRButton;
15 | protected TextView mResultView;
16 | protected String TAG;
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_base);
22 | mLButton = (Button) findViewById(R.id.left);
23 | mRButton = (Button) findViewById(R.id.right);
24 | mResultView = (TextView) findViewById(R.id.result);
25 | TAG = getLocalClassName();
26 | }
27 |
28 | protected void log(Object s) {
29 | Log.i(TAG, String.valueOf(s));
30 | Observable.just(s).observeOn(AndroidSchedulers.mainThread()).subscribe(i -> {
31 | mResultView.setText(mResultView.getText() + "\n" + i);
32 | });
33 |
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 |
4 | android {
5 | compileSdkVersion 23
6 | buildToolsVersion '25.0.0'
7 | compileOptions {
8 | sourceCompatibility JavaVersion.VERSION_1_8
9 | targetCompatibility JavaVersion.VERSION_1_8
10 | }
11 | defaultConfig {
12 | applicationId "cn.com.chaoba.rxjavademo"
13 | minSdkVersion 15
14 | targetSdkVersion 23
15 | versionCode 1
16 | versionName "1.0"
17 | jackOptions {
18 | enabled true
19 | }
20 | }
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 | compileOptions {
28 | sourceCompatibility JavaVersion.VERSION_1_8
29 | targetCompatibility JavaVersion.VERSION_1_8
30 | }
31 | }
32 | repositories {
33 | flatDir {
34 | dirs 'libs'
35 | }
36 | }
37 | dependencies {
38 | compile fileTree(include: ['*.jar'], dir: 'libs')
39 | compile 'com.android.support:appcompat-v7:23.0.0'
40 | compile(name:'rxandroid-1.2.1', ext:'aar')
41 | // compile 'io.reactivex:rxjava:1.2.2'
42 | // compile('io.reactivex:rxandroid:1.2.1')
43 | // {
44 | // exclude module: 'rxjava'
45 | // }
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/connectable/RefCountActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.connectable;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscription;
10 | import rx.functions.Action1;
11 | import rx.observables.ConnectableObservable;
12 | import rx.schedulers.Schedulers;
13 |
14 | public class RefCountActivity extends BaseActivity {
15 | Subscription subscription;
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | ConnectableObservable obs = publishObserver();
21 |
22 | mLButton.setText("refCount");
23 | mLButton.setOnClickListener(e -> {
24 | subscription = obs.refCount().subscribe(new Action1() {
25 | @Override
26 | public void call(Long aLong) {
27 | log("refCount:" + aLong);
28 | }
29 | });
30 |
31 | });
32 | mRButton.setText("stop");
33 | mRButton.setOnClickListener(e -> {
34 | subscription.unsubscribe();
35 | });
36 | }
37 |
38 | private ConnectableObservable publishObserver() {
39 | Observable obser = Observable.interval(1, TimeUnit.SECONDS);
40 | obser.observeOn(Schedulers.newThread());
41 | return obser.publish();
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/transforming/ScanActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.transforming;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.ArrayList;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import rx.Observable;
10 | import rx.functions.Action1;
11 | import rx.functions.Func2;
12 |
13 | public class ScanActivity extends BaseActivity {
14 | ArrayList list = new ArrayList<>(2);
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | for (int i = 0; i < 10; i++) {
20 | list.add(2);
21 | }
22 | mLButton.setText("scan");
23 | mLButton.setOnClickListener(new View.OnClickListener() {
24 | @Override
25 | public void onClick(View v) {
26 | scanObserver().subscribe(new Action1() {
27 | @Override
28 | public void call(Integer i) {
29 | log("scan:" + i);
30 | }
31 | });
32 | }
33 | });
34 |
35 |
36 | }
37 |
38 | private Observable scanObserver() {
39 | return Observable.from(list).scan(new Func2() {
40 | @Override
41 | public Integer call(Integer x, Integer y) {
42 | return x * y;
43 | }
44 | });
45 | }
46 |
47 | }
48 |
49 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/conditional_boolean/SequenceEqualActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.conditional_boolean;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.functions.Action1;
8 |
9 | public class SequenceEqualActivity extends BaseActivity {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 | mLButton.setText("equal");
15 | mLButton.setOnClickListener(e -> {
16 | equalObserver().subscribe(new Action1() {
17 | @Override
18 | public void call(Boolean i) {
19 | log("equal:" + i);
20 | }
21 | });
22 | });
23 | mRButton.setText("notEqual");
24 | mRButton.setOnClickListener(e -> {
25 | notEqualObserver().subscribe(new Action1() {
26 | @Override
27 | public void call(Boolean i) {
28 | log("notEqual:" + i);
29 | }
30 | });
31 | });
32 | }
33 |
34 | private Observable equalObserver() {
35 | return Observable.sequenceEqual(Observable.just(1, 2, 3),
36 | Observable.just(1, 2, 3));
37 | }
38 |
39 | private Observable notEqualObserver() {
40 | return Observable.sequenceEqual(Observable.just(1, 2, 3),
41 | Observable.just(1, 2));
42 | }
43 | }
44 |
45 |
46 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/utility/MaterializeActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.utility;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Notification;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 |
10 | public class MaterializeActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("materialize");
16 | mLButton.setOnClickListener(e -> {
17 | materializeObserver().subscribe(new Action1>() {
18 | @Override
19 | public void call(Notification i) {
20 | log("materialize:" + i.getValue() + " type" + i.getKind());
21 | }
22 | });
23 | });
24 | mRButton.setText("deMeterialize");
25 | mRButton.setOnClickListener(e -> {
26 | deMaterializeObserver().subscribe(new Action1() {
27 | @Override
28 | public void call(Integer i) {
29 | log("deMeterialize:" + i);
30 | }
31 | });
32 | });
33 | }
34 |
35 | private Observable> materializeObserver() {
36 | return Observable.just(1, 2, 3).materialize();
37 | }
38 |
39 | private Observable deMaterializeObserver() {
40 | return materializeObserver().dematerialize();
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/aggregate/ConcatAndCountActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.aggregate;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.functions.Action1;
8 |
9 |
10 | public class ConcatAndCountActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("concat");
16 | mLButton.setOnClickListener(e -> {
17 | concatObserver().subscribe(new Action1() {
18 | @Override
19 | public void call(Integer i) {
20 | log("concat:" + i);
21 | }
22 | });
23 | });
24 | mRButton.setText("count");
25 | mRButton.setOnClickListener(e -> {
26 | countObserver().subscribe(new Action1() {
27 | @Override
28 | public void call(Integer i) {
29 | log("count:" + i);
30 | }
31 | });
32 | });
33 | }
34 |
35 | private Observable concatObserver() {
36 | Observable obser1 = Observable.just(1, 2, 3);
37 | Observable obser2 = Observable.just(4, 5, 6);
38 | Observable obser3 = Observable.just(7, 8, 9);
39 | return Observable.concat(obser1, obser2, obser3);
40 | }
41 |
42 | private Observable countObserver() {
43 | return Observable.just(1, 2, 3).count();
44 | }
45 | }
46 |
47 |
48 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/conditional_boolean/SkipUntilSkipWhileActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.conditional_boolean;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 |
11 | public class SkipUntilSkipWhileActivity extends BaseActivity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | mLButton.setText("skipUntil");
17 | mLButton.setOnClickListener(e -> {
18 | skipUntilObserver().subscribe(new Action1() {
19 | @Override
20 | public void call(Long i) {
21 | log("skipUntil:" + i);
22 | }
23 | });
24 | });
25 | mRButton.setText("skipWhile");
26 | mRButton.setOnClickListener(e -> {
27 | skipWhileObserver().subscribe(new Action1() {
28 | @Override
29 | public void call(Long i) {
30 | log("skipWhile:" + i);
31 | }
32 | });
33 | });
34 | }
35 |
36 | private Observable skipUntilObserver() {
37 | return Observable.interval(1, TimeUnit.SECONDS)
38 | .skipUntil(Observable.timer(3, TimeUnit.SECONDS));
39 | }
40 |
41 | private Observable skipWhileObserver() {
42 | return Observable.interval(1, TimeUnit.SECONDS)
43 | .skipWhile(aLong -> aLong < 5);
44 | }
45 | }
46 |
47 |
48 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/IntervalActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscriber;
10 | import rx.android.schedulers.AndroidSchedulers;
11 |
12 | public class IntervalActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | Observable observable = interval();
18 |
19 | Subscriber subscriber = new Subscriber() {
20 | @Override
21 | public void onCompleted() {
22 | log("onCompleted" );
23 | }
24 |
25 | @Override
26 | public void onError(Throwable e) {
27 | log("onError:" + e.getMessage());
28 | }
29 |
30 | @Override
31 | public void onNext(Long aLong) {
32 | log(aLong);
33 | }
34 |
35 | };
36 | mLButton.setText("Interval");
37 | mRButton.setText("UnSubsCribe");
38 | mLButton.setOnClickListener(e -> observable.subscribe(subscriber));
39 | mRButton.setOnClickListener(e -> subscriber.unsubscribe());
40 | }
41 |
42 | private Observable interval() {
43 | return Observable.interval(1, TimeUnit.SECONDS)
44 | //interva operates by default on the computation Scheduler,so observe on main Thread
45 | .observeOn(AndroidSchedulers.mainThread());
46 | }
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/conditional_boolean/DefaultIfEmptyActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.conditional_boolean;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.Subscriber;
8 | import rx.functions.Action1;
9 |
10 | public class DefaultIfEmptyActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("empty");
16 | mLButton.setOnClickListener(e -> {
17 | emptyObserver().subscribe(new Action1() {
18 | @Override
19 | public void call(Integer i) {
20 | log("empty:" + i);
21 | }
22 | });
23 | });
24 | mRButton.setText("notEmpty");
25 | mRButton.setOnClickListener(e -> {
26 | notEmptyObserver().subscribe(new Action1() {
27 | @Override
28 | public void call(Integer i) {
29 | log("notEmpty:" + i);
30 | }
31 | });
32 | });
33 | }
34 |
35 | private Observable emptyObserver() {
36 | return Observable.create(new Observable.OnSubscribe() {
37 | @Override
38 | public void call(Subscriber super Integer> subscriber) {
39 | subscriber.onCompleted();
40 | }
41 | }).defaultIfEmpty(10);
42 | }
43 |
44 | private Observable notEmptyObserver() {
45 | return Observable.just(1).defaultIfEmpty(10);
46 | }
47 | }
48 |
49 |
50 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/Filtering/SkipAndTakeActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.filtering;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 |
10 | public class SkipAndTakeActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("Skip");
16 | mLButton.setOnClickListener(new View.OnClickListener() {
17 | @Override
18 | public void onClick(View v) {
19 | skipObserver().subscribe(new Action1() {
20 | @Override
21 | public void call(Integer i) {
22 | log("Skip:" + i);
23 | }
24 | });
25 | }
26 | });
27 | mRButton.setText("Take");
28 | mRButton.setOnClickListener(new View.OnClickListener() {
29 | @Override
30 | public void onClick(View v) {
31 | takeObserver().subscribe(new Action1() {
32 | @Override
33 | public void call(Integer i) {
34 | log("Take:" + i);
35 | }
36 | });
37 | }
38 | });
39 | }
40 |
41 | private Observable skipObserver() {
42 | return Observable.just(0, 1, 2, 3, 4, 5).skip(2);
43 | }
44 |
45 | private Observable takeObserver() {
46 | return Observable.just(0, 1, 2, 3, 4, 5).take(2);
47 | }
48 |
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/scheduler/FromExecutorActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.scheduler;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.Executor;
6 | import java.util.concurrent.LinkedBlockingQueue;
7 | import java.util.concurrent.ThreadFactory;
8 | import java.util.concurrent.ThreadPoolExecutor;
9 | import java.util.concurrent.TimeUnit;
10 |
11 | import cn.com.chaoba.rxjavademo.BaseActivity;
12 | import rx.Observable;
13 | import rx.Scheduler;
14 | import rx.functions.Action1;
15 | import rx.schedulers.Schedulers;
16 |
17 | public class FromExecutorActivity extends BaseActivity {
18 |
19 | class SimpleThreadFactory implements ThreadFactory {
20 | public Thread newThread(Runnable r) {
21 | return new Thread(r);
22 | }
23 | }
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | Executor executor = new ThreadPoolExecutor(
29 | 2, //corePoolSize
30 | 2, //maximumPoolSize
31 | 2000L, TimeUnit.MILLISECONDS, //keepAliveTime, unit
32 | new LinkedBlockingQueue<>(1000), //workQueue
33 | new SimpleThreadFactory()
34 | );
35 | Scheduler scheduler = Schedulers.from(executor);
36 | Observable.interval(1, TimeUnit.SECONDS).take(5)
37 | .observeOn(scheduler)
38 | .subscribe(new Action1() {
39 | @Override
40 | public void call(Long l) {
41 | log(l + "-" + Thread.currentThread().getName());
42 | }
43 | });
44 |
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/Filtering/DistinctActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.filtering;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 |
10 | public class DistinctActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("distinct");
16 | mLButton.setOnClickListener(new View.OnClickListener() {
17 | @Override
18 | public void onClick(View v) {
19 | distinctObserver().subscribe(new Action1() {
20 | @Override
21 | public void call(Integer i) {
22 | log("distinct:" + i);
23 | }
24 | });
25 | }
26 | });
27 | mRButton.setText("UntilChanged");
28 | mRButton.setOnClickListener(new View.OnClickListener() {
29 | @Override
30 | public void onClick(View v) {
31 | distinctUntilChangedObserver().subscribe(new Action1() {
32 | @Override
33 | public void call(Integer i) {
34 | log("UntilChanged:" + i);
35 | }
36 | });
37 | }
38 | });
39 | }
40 |
41 | private Observable distinctObserver() {
42 | return Observable.just(1, 2, 3, 4, 5, 4, 3, 2, 1).distinct();
43 |
44 | }
45 |
46 | private Observable distinctUntilChangedObserver() {
47 | return Observable.just(1, 2, 3, 3, 3, 1, 2, 3, 3).distinctUntilChanged();
48 |
49 | }
50 |
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/utility/TimeIntervalTimeStampActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.utility;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.schedulers.TimeInterval;
11 | import rx.schedulers.Timestamped;
12 |
13 | public class TimeIntervalTimeStampActivity extends BaseActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | mLButton.setText("timeInterval");
19 | mLButton.setOnClickListener(e -> {
20 | timeIntervalObserver().subscribe(new Action1>() {
21 | @Override
22 | public void call(TimeInterval i) {
23 | log("timeInterval:" + i.getValue() + "-"
24 | + i.getIntervalInMilliseconds());
25 | }
26 | });
27 | });
28 | mRButton.setText("timeStamp");
29 | mRButton.setOnClickListener(e -> {
30 | timeStampObserver().subscribe(new Action1>() {
31 | @Override
32 | public void call(Timestamped i) {
33 | log("timeStamp:" + i.getValue() + "-"
34 | + i.getTimestampMillis());
35 | }
36 | });
37 | });
38 | }
39 |
40 | private Observable> timeIntervalObserver() {
41 | return Observable.interval(1, TimeUnit.SECONDS).take(3).timeInterval();
42 | }
43 |
44 | private Observable> timeStampObserver() {
45 | return Observable.interval(1, TimeUnit.SECONDS).take(3).timestamp();
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/conditional_boolean/TakeUntilTakeWhileActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.conditional_boolean;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.functions.Func1;
11 |
12 | public class TakeUntilTakeWhileActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | mLButton.setText("takeUntil");
18 | mLButton.setOnClickListener(e -> {
19 | takeUntilObserver().subscribe(new Action1() {
20 | @Override
21 | public void call(Long i) {
22 | log("takeUntil:" + i);
23 | }
24 | });
25 | });
26 | mRButton.setText("takeWhile");
27 | mRButton.setOnClickListener(e -> {
28 | takeWhileObserver().subscribe(new Action1() {
29 | @Override
30 | public void call(Long i) {
31 | log("takeWhile:" + i);
32 | }
33 | });
34 | });
35 | }
36 |
37 | private Observable takeUntilObserver() {
38 | return Observable.interval(1, TimeUnit.SECONDS)
39 | .takeUntil(Observable.timer(3, TimeUnit.SECONDS));
40 | }
41 |
42 | private Observable takeWhileObserver() {
43 | return Observable.interval(1, TimeUnit.SECONDS)
44 | .takeWhile(new Func1() {
45 | @Override
46 | public Boolean call(Long aLong) {
47 | return aLong < 5;
48 | }
49 | });
50 | }
51 | }
52 |
53 |
54 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/connectable/PublishAndConnectActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.connectable;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscription;
10 | import rx.functions.Action1;
11 | import rx.observables.ConnectableObservable;
12 | import rx.schedulers.Schedulers;
13 |
14 | public class PublishAndConnectActivity extends BaseActivity {
15 | Subscription mSubscription;
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | ConnectableObservable obs = publishObserver();
21 | Action1 action2 = new Action1() {
22 | @Override
23 | public void call(Object o) {
24 | log("action2:" + o);
25 | }
26 | };
27 | Action1 action1 = new Action1() {
28 | @Override
29 | public void call(Object o) {
30 | log("action1:" + o);
31 | if ((long) o == 3) obs.subscribe(action2);
32 | }
33 | };
34 | obs.subscribe(action1);
35 |
36 | mLButton.setText("start");
37 | mLButton.setOnClickListener(e -> {
38 | mSubscription = obs.connect();
39 | });
40 | mRButton.setText("stop");
41 | mRButton.setOnClickListener(e -> {
42 | if (mSubscription != null) {
43 | mSubscription.unsubscribe();
44 | }
45 | });
46 | }
47 |
48 | private ConnectableObservable publishObserver() {
49 | Observable obser = Observable.interval(1, TimeUnit.SECONDS);
50 | obser.observeOn(Schedulers.newThread());
51 | return obser.publish();
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/Filtering/SampleActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.filtering;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import rx.Observable;
10 | import rx.functions.Action1;
11 |
12 | public class SampleActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | mLButton.setText("sample");
18 | mLButton.setOnClickListener(new View.OnClickListener() {
19 | @Override
20 | public void onClick(View v) {
21 | sampleObserver().subscribe(new Action1() {
22 | @Override
23 | public void call(Long i) {
24 | log("sample:" + i);
25 | }
26 | });
27 | }
28 | });
29 | mRButton.setText("throttleFirst");
30 | mRButton.setOnClickListener(new View.OnClickListener() {
31 | @Override
32 | public void onClick(View v) {
33 | throttleFirstObserver().subscribe(new Action1() {
34 | @Override
35 | public void call(Long i) {
36 | log("throttleFirst:" + i);
37 | }
38 | });
39 | }
40 | });
41 | }
42 |
43 | private Observable sampleObserver() {
44 | return Observable.interval(200, TimeUnit.MILLISECONDS)
45 | .sample(1000, TimeUnit.MILLISECONDS);
46 | }
47 |
48 | private Observable throttleFirstObserver() {
49 | return Observable.interval(200, TimeUnit.MILLISECONDS)
50 | .throttleFirst(1000, TimeUnit.MILLISECONDS);
51 | }
52 |
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/FromActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | import cn.com.chaoba.rxjavademo.BaseActivity;
10 | import rx.Observable;
11 | import rx.functions.Action1;
12 |
13 | public class FromActivity extends BaseActivity {
14 | Integer[] arrays = {0, 1, 2, 3, 4, 5};
15 | List list = new ArrayList<>();
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | for (int i = 0; i <= 5; i++) {
21 | list.add(i);
22 | }
23 | mLButton.setText("FromArray");
24 | mRButton.setText("FromIterable");
25 | mLButton.setOnClickListener(new View.OnClickListener() {
26 | @Override
27 | public void onClick(View v) {
28 | FromArray().subscribe(new Action1() {
29 | @Override
30 | public void call(Integer i) {
31 | log("FromArray:" + i);
32 | }
33 | });
34 | }
35 | });
36 | mRButton.setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | FromIterable().subscribe(new Action1() {
40 | @Override
41 | public void call(Integer i) {
42 | log("FromIterable:" + i);
43 | }
44 | });
45 | }
46 | });
47 | }
48 |
49 | private Observable FromArray() {
50 | return Observable.from(arrays);
51 | }
52 |
53 | private Observable FromIterable() {
54 | return Observable.from(list);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/RepeatAndTimerActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import rx.Observable;
10 | import rx.android.schedulers.AndroidSchedulers;
11 | import rx.functions.Action1;
12 |
13 |
14 | public class RepeatAndTimerActivity extends BaseActivity {
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | mLButton.setText("repeat");
20 | mLButton.setOnClickListener(new View.OnClickListener() {
21 | @Override
22 | public void onClick(View v) {
23 | repeatObserver().subscribe(new Action1() {
24 | @Override
25 | public void call(Integer i) {
26 | log("repeat:" + i);
27 | }
28 | });
29 | }
30 | });
31 | mRButton.setText("timer");
32 | mRButton.setOnClickListener(new View.OnClickListener() {
33 | @Override
34 | public void onClick(View v) {
35 | timerObserver().subscribe(new Action1() {
36 | @Override
37 | public void call(Long aLong) {
38 | log("timer:" + aLong);
39 | }
40 | });
41 | }
42 | });
43 | }
44 |
45 | private Observable repeatObserver() {
46 | return Observable.just(1, 2, 3).repeat(3);
47 | }
48 |
49 | private Observable timerObserver() {
50 | //timer by default operates on the computation Scheduler
51 | return Observable.timer(1, TimeUnit.SECONDS)
52 | .observeOn(AndroidSchedulers.mainThread());
53 | }
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/Filtering/ElementAtAndFilterActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.filtering;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 | import rx.functions.Func1;
10 |
11 | public class ElementAtAndFilterActivity extends BaseActivity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | mLButton.setText("elementAt");
17 | mLButton.setOnClickListener(new View.OnClickListener() {
18 | @Override
19 | public void onClick(View v) {
20 | elementAtObserver().subscribe(new Action1() {
21 | @Override
22 | public void call(Integer i) {
23 | log("elementAt:" + i);
24 | }
25 | });
26 | }
27 | });
28 | mRButton.setText("Filter");
29 | mRButton.setOnClickListener(new View.OnClickListener() {
30 | @Override
31 | public void onClick(View v) {
32 | filterObserver().subscribe(new Action1() {
33 | @Override
34 | public void call(Integer i) {
35 | log("Filter:" + i);
36 | }
37 | });
38 | }
39 | });
40 | }
41 |
42 | private Observable elementAtObserver() {
43 | return Observable.just(0, 1, 2, 3, 4, 5).elementAt(2);
44 | }
45 |
46 | private Observable filterObserver() {
47 | return Observable.just(0, 1, 2, 3, 4, 5).filter(new Func1() {
48 | @Override
49 | public Boolean call(Integer integer) {
50 | return integer < 3;
51 | }
52 | });
53 | }
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/conditional_boolean/ContainsActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.conditional_boolean;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.Subscriber;
8 | import rx.functions.Action1;
9 |
10 | public class ContainsActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("contains");
16 | mLButton.setOnClickListener(e -> {
17 | containsObserver().subscribe(new Action1() {
18 | @Override
19 | public void call(Boolean i) {
20 | log("contains:" + i);
21 | }
22 | });
23 | notContainsObserver().subscribe(new Action1() {
24 | @Override
25 | public void call(Boolean i) {
26 | log("not contains:" + i);
27 | }
28 | });
29 | });
30 | mRButton.setText("isEmpty");
31 | mRButton.setOnClickListener(e -> {
32 | emptyObserver().subscribe(new Action1() {
33 | @Override
34 | public void call(Boolean i) {
35 | log("isEmpty:" + i);
36 | }
37 | });
38 | });
39 | }
40 |
41 | private Observable containsObserver() {
42 | return Observable.just(1, 2, 3).contains(3);
43 | }
44 |
45 | private Observable notContainsObserver() {
46 | return Observable.just(1, 2, 3).contains(4);
47 | }
48 |
49 | private Observable emptyObserver() {
50 | return Observable.create(new Observable.OnSubscribe() {
51 | @Override
52 | public void call(Subscriber super Integer> subscriber) {
53 | subscriber.onCompleted();
54 | }
55 | }).isEmpty();
56 | }
57 | }
58 |
59 |
60 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/transforming/BufferActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.transforming;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.List;
7 | import java.util.concurrent.TimeUnit;
8 |
9 | import cn.com.chaoba.rxjavademo.BaseActivity;
10 | import rx.Observable;
11 | import rx.android.schedulers.AndroidSchedulers;
12 | import rx.functions.Action1;
13 |
14 | public class BufferActivity extends BaseActivity {
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | mLButton.setText("buffer");
20 | mLButton.setOnClickListener(new View.OnClickListener() {
21 | @Override
22 | public void onClick(View v) {
23 | bufferObserver().subscribe(new Action1>() {
24 | @Override
25 | public void call(List integers) {
26 | log("buffer:" + integers);
27 | }
28 | });
29 | }
30 | });
31 | mRButton.setText("bufferTime");
32 | mRButton.setOnClickListener(new View.OnClickListener() {
33 | @Override
34 | public void onClick(View v) {
35 | bufferTimeObserver().subscribe(new Action1>() {
36 | @Override
37 | public void call(List longs) {
38 | log("bufferTime:" + longs);
39 | }
40 | });
41 | }
42 | });
43 | }
44 |
45 | private Observable> bufferObserver() {
46 | return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
47 | .buffer(2, 3);
48 | }
49 |
50 | private Observable> bufferTimeObserver() {
51 | return Observable.interval(1, TimeUnit.SECONDS)
52 | .buffer(3, TimeUnit.SECONDS)
53 | .observeOn(AndroidSchedulers.mainThread());
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/Filtering/FirstActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.filtering;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 | import rx.functions.Func1;
10 |
11 | public class FirstActivity extends BaseActivity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | mLButton.setText("First");
17 | mLButton.setOnClickListener(
18 | new View.OnClickListener() {
19 | @Override
20 | public void onClick(View v) {
21 | Observable.just(0, 1, 2, 3, 4, 5).first(new Func1() {
22 | @Override
23 | public Boolean call(Integer integer) {
24 | return integer > 3;
25 | }
26 | }).subscribe(new Action1() {
27 | @Override
28 | public void call(Integer i) {
29 | log("First:" + i);
30 | }
31 | });
32 | }
33 | });
34 |
35 |
36 | mRButton.setText("Blocking");
37 | mRButton.setOnClickListener(new View.OnClickListener() {
38 | @Override
39 | public void onClick(View v) {
40 | int result = Observable.just(0, 1, 2, 3, 4, 5)
41 | .toBlocking()
42 | .first(new Func1() {
43 | @Override
44 | public Boolean call(Integer i) {
45 | return i > 1;
46 | }
47 | });
48 | log("blocking:" + result);
49 | }
50 | });
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_base.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
18 |
19 |
26 |
27 |
35 |
36 |
37 |
38 |
39 |
43 |
44 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/DeferAndJustActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.NonNull;
5 | import android.view.View;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.functions.Func0;
11 |
12 | public class DeferAndJustActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | Observable deferObservable = getDefer();
18 | Observable justObservable = getJust();
19 | mLButton.setText("Defer");
20 | mRButton.setText("Just");
21 | mLButton.setOnClickListener(new View.OnClickListener() {
22 | @Override
23 | public void onClick(View v) {
24 | deferObservable.subscribe(new Action1() {
25 | @Override
26 | public void call(Long time) {
27 | log("defer:" + time);
28 | }
29 | });
30 | }
31 | });
32 |
33 | mRButton.setOnClickListener(new View.OnClickListener() {
34 | @Override
35 | public void onClick(View v) {
36 | justObservable.subscribe(new Action1() {
37 | @Override
38 | public void call(Long time) {
39 | log("just:" + time);
40 | }
41 | });
42 | }
43 | });
44 | }
45 |
46 | @NonNull
47 | private Observable getJust() {
48 | return Observable.just(System.currentTimeMillis());
49 | }
50 |
51 | @NonNull
52 | private Observable getDefer() {
53 | return Observable.defer(new Func0>() {
54 | @Override
55 | public Observable call() {
56 | return getJust();
57 | }
58 | });
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/connectable/ReplayActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.connectable;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscription;
10 | import rx.functions.Action1;
11 | import rx.observables.ConnectableObservable;
12 | import rx.schedulers.Schedulers;
13 |
14 | public class ReplayActivity extends BaseActivity {
15 | Subscription mSubscription;
16 | ConnectableObservable obs;
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 |
22 | Action1 action2 = new Action1() {
23 | @Override
24 | public void call(Object o) {
25 | log("action2:" + o);
26 | }
27 | };
28 | Action1 action1 = new Action1() {
29 | @Override
30 | public void call(Object o) {
31 | log("action1:" + o);
32 | if ((long) o == 3) obs.subscribe(action2);
33 | }
34 | };
35 |
36 |
37 | mLButton.setText("relayCount");
38 | mLButton.setOnClickListener(e -> {
39 | obs = relayCountObserver();
40 | obs.subscribe(action1);
41 | log("relayCount");
42 | mSubscription = obs.connect();
43 | });
44 | mRButton.setText("relayTime");
45 | mRButton.setOnClickListener(e -> {
46 | obs = relayTimeObserver();
47 | obs.subscribe(action1);
48 | log("relayTime");
49 | mSubscription = obs.connect();
50 | });
51 | }
52 |
53 | private ConnectableObservable relayCountObserver() {
54 | Observable obser = Observable.interval(1, TimeUnit.SECONDS);
55 | obser.observeOn(Schedulers.newThread());
56 | return obser.replay(2);
57 | }
58 |
59 | private ConnectableObservable relayTimeObserver() {
60 | Observable obser = Observable.interval(1, TimeUnit.SECONDS);
61 | obser.observeOn(Schedulers.newThread());
62 | return obser.replay(3, TimeUnit.SECONDS);
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/combining/StartWithAndSwitchActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.combining;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.functions.Func1;
11 |
12 | public class StartWithAndSwitchActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | mLButton.setText("StartWith");
18 | mLButton.setOnClickListener(e -> {
19 | startWithObserver().subscribe(new Action1() {
20 | @Override
21 | public void call(Integer i) {
22 | log("StartWith:" + i);
23 | }
24 | });
25 | });
26 | mRButton.setText("switch");
27 | mRButton.setOnClickListener(e -> {
28 | switchObserver().subscribe(new Action1() {
29 | @Override
30 | public void call(String s) {
31 | log("switch:" + s);
32 | }
33 | });
34 | });
35 | }
36 |
37 | private Observable startWithObserver() {
38 | return Observable.just(1, 2, 3).startWith(-1, 0);
39 | }
40 |
41 | private Observable createObserver(Long index) {
42 | return Observable.interval(1000, 1000, TimeUnit.MILLISECONDS).take(5)
43 | .map(new Func1() {
44 | @Override
45 | public String call(Long aLong) {
46 | return index + "-" + aLong;
47 | }
48 | });
49 | }
50 |
51 | private Observable switchObserver() {
52 | return Observable.switchOnNext(Observable
53 | .interval(3000, TimeUnit.MILLISECONDS)
54 | .take(3)
55 | .map(new Func1>() {
56 | @Override
57 | public Observable call(Long aLong) {
58 | return createObserver(aLong);
59 | }
60 | }));
61 | }
62 |
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/utility/DelayActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.utility;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscriber;
10 | import rx.functions.Action1;
11 | import rx.schedulers.Schedulers;
12 |
13 | public class DelayActivity extends BaseActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | mLButton.setText("delay");
19 | mLButton.setOnClickListener(e -> {
20 | log("start subscribe:" + getCurrentTime());
21 | delayObserver().subscribe(new Action1() {
22 | @Override
23 | public void call(Long i) {
24 | log("delay:" + (getCurrentTime() - i));
25 | }
26 | });
27 | });
28 | mRButton.setText("delaySubscription");
29 | mRButton.setOnClickListener(e -> {
30 | log("start subscribe:" + getCurrentTime());
31 | delaySubscriptionObserver().subscribe(new Action1() {
32 | @Override
33 | public void call(Long i) {
34 | log("delaySubscription:" + getCurrentTime());
35 | }
36 | });
37 | });
38 | }
39 |
40 | private Observable delayObserver() {
41 | return createObserver(2).delay(2000, TimeUnit.MILLISECONDS);
42 | }
43 |
44 | private Observable delaySubscriptionObserver() {
45 | return createObserver(2)
46 | .delaySubscription(2000, TimeUnit.MILLISECONDS);
47 | }
48 |
49 | private Observable createObserver(int index) {
50 | return Observable.create(new Observable.OnSubscribe() {
51 | @Override
52 | public void call(Subscriber super Long> subscriber) {
53 | log("subscribe:" + getCurrentTime());
54 | for (int i = 1; i <= index; i++) {
55 | subscriber.onNext(getCurrentTime());
56 | }
57 | }
58 | }).subscribeOn(Schedulers.newThread());
59 | }
60 |
61 | private long getCurrentTime() {
62 | return System.currentTimeMillis() / 1000;
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/CompletableActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Completable;
8 | import rx.Subscription;
9 |
10 | public class CompletableActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("Error");
16 | mLButton.setOnClickListener(new View.OnClickListener() {
17 | @Override
18 | public void onClick(View v) {
19 | Completable.error(new Throwable("Completable error"))
20 | .subscribe(new Completable.CompletableSubscriber() {
21 | @Override
22 | public void onCompleted() {
23 | log("onCompleted");
24 | }
25 |
26 | @Override
27 | public void onError(Throwable e) {
28 | log(e.getMessage());
29 | }
30 |
31 | @Override
32 | public void onSubscribe(Subscription d) {
33 | }
34 | });
35 | }
36 | });
37 | mRButton.setText("complete");
38 | mRButton.setOnClickListener(new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | Completable.complete()
42 | .subscribe(new Completable.CompletableSubscriber() {
43 | @Override
44 | public void onCompleted() {
45 | log("onCompleted");
46 | }
47 |
48 | @Override
49 | public void onError(Throwable e) {
50 | log(e.getMessage());
51 | }
52 |
53 | @Override
54 | public void onSubscribe(Subscription d) {
55 | }
56 | });
57 | }
58 | });
59 | }
60 |
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/aggregate/Reducectivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.aggregate;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.ArrayList;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.functions.Action2;
11 | import rx.functions.Func0;
12 | import rx.functions.Func2;
13 |
14 | public class Reducectivity extends BaseActivity {
15 | ArrayList list = new ArrayList<>();
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | for (int i = 0; i < 10; i++) {
21 | list.add(2);
22 | }
23 | mLButton.setText("reduce");
24 | mLButton.setOnClickListener(e -> {
25 | reduceObserver().subscribe(new Action1() {
26 | @Override
27 | public void call(Integer i) {
28 | log("reduce:" + i);
29 | }
30 | });
31 | });
32 | mRButton.setText("collect");
33 | mRButton.setOnClickListener(e -> {
34 | collectObserver().subscribe(new Action1>() {
35 | @Override
36 | public void call(ArrayList integers) {
37 | log("collect:" + integers);
38 | }
39 | });
40 | });
41 | }
42 |
43 | private Observable reduceObserver() {
44 | return Observable.from(list).reduce((new Func2() {
45 | @Override
46 | public Integer call(Integer x, Integer y) {
47 | return x * y;
48 | }
49 | }));
50 | }
51 |
52 | private Observable> collectObserver() {
53 | return Observable.from(list).collect(
54 | new Func0>() {
55 | @Override
56 | public ArrayList call() {
57 | return new ArrayList<>();
58 | }
59 | }, new Action2, Integer>() {
60 | @Override
61 | public void call(ArrayList integers, Integer integer) {
62 | integers.add(integer);
63 | }
64 | });
65 | }
66 |
67 | }
68 |
69 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/transforming/MapAndCastActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.transforming;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 | import rx.functions.Func1;
10 |
11 | public class MapAndCastActivity extends BaseActivity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | mLButton.setText("Map");
17 | mLButton.setOnClickListener(new View.OnClickListener() {
18 | @Override
19 | public void onClick(View v) {
20 | mapObserver().subscribe(new Action1() {
21 | @Override
22 | public void call(Integer integer) {
23 | log("Map:" + integer);
24 | }
25 | });
26 | }
27 | });
28 | mRButton.setText("Cast");
29 | mRButton.setOnClickListener(new View.OnClickListener() {
30 | @Override
31 | public void onClick(View v) {
32 | castObserver().subscribe(new Action1() {
33 | @Override
34 | public void call(Dog dog) {
35 | log("Cast:" + dog.getName());
36 | }
37 | });
38 | }
39 | });
40 | }
41 |
42 | private Observable mapObserver() {
43 | return Observable.just(1, 2, 3)
44 | .map(new Func1() {
45 | @Override
46 | public Integer call(Integer integer) {
47 | return integer * 10;
48 | }
49 | });
50 | }
51 |
52 | private Observable castObserver() {
53 | return Observable.just(getAnimal())
54 | .cast(Dog.class);
55 | }
56 |
57 | Animal getAnimal() {
58 | return new Dog();
59 | }
60 |
61 | class Animal {
62 | protected String name = "Animal";
63 |
64 | Animal() {
65 | log("create " + name);
66 | }
67 |
68 | String getName() {
69 | return name;
70 | }
71 | }
72 |
73 | class Dog extends Animal {
74 | Dog() {
75 | name = getClass().getSimpleName();
76 | log("create " + name);
77 | }
78 |
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/utility/TimeoutActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.utility;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscriber;
10 | import rx.functions.Action1;
11 |
12 | public class TimeoutActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | mLButton.setText("timeout");
18 | mLButton.setOnClickListener(e -> {
19 | timeoutObserver().subscribe(new Subscriber() {
20 | @Override
21 | public void onCompleted() {
22 |
23 | }
24 |
25 | @Override
26 | public void onError(Throwable e) {
27 | log(e);
28 | }
29 |
30 | @Override
31 | public void onNext(Integer integer) {
32 | log("timeout:" + integer);
33 | }
34 | });
35 | });
36 | mRButton.setText("timeoutobserver");
37 | mRButton.setOnClickListener(e -> {
38 | timeoutobserverObserver().subscribe(new Action1() {
39 | @Override
40 | public void call(Integer integer) {
41 | log(integer);
42 | }
43 | });
44 | });
45 | }
46 |
47 | private Observable timeoutObserver() {
48 | return createObserver().timeout(200, TimeUnit.MILLISECONDS);
49 | }
50 |
51 | private Observable timeoutobserverObserver() {
52 | return createObserver()
53 | .timeout(200, TimeUnit.MILLISECONDS, Observable.just(5, 6));
54 | }
55 |
56 | private Observable createObserver() {
57 | return Observable.create(new Observable.OnSubscribe() {
58 | @Override
59 | public void call(Subscriber super Integer> subscriber) {
60 | for (int i = 0; i <= 3; i++) {
61 | try {
62 | Thread.sleep(i * 100);
63 | } catch (InterruptedException e) {
64 | e.printStackTrace();
65 | }
66 | subscriber.onNext(i);
67 | }
68 | subscriber.onCompleted();
69 | }
70 | });
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/combining/ZipActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.combining;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.functions.Func1;
11 | import rx.functions.Func2;
12 | import rx.functions.Func3;
13 |
14 | public class ZipActivity extends BaseActivity {
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | mLButton.setText("zipWith");
20 | mLButton.setOnClickListener(e -> {
21 | zipWithObserver().subscribe(new Action1() {
22 | @Override
23 | public void call(String s) {
24 | log("zipWith:" + s);
25 | }
26 | });
27 | });
28 | mRButton.setText("zip");
29 | mRButton.setOnClickListener(e -> {
30 | zipWithIterableObserver().subscribe(new Action1() {
31 | @Override
32 | public void call(String s) {
33 | log("zip:" + s);
34 | }
35 | });
36 | });
37 | }
38 |
39 | private Observable zipWithObserver() {
40 | return createObserver(2).zipWith(createObserver(3),
41 | new Func2() {
42 | @Override
43 | public String call(String s, String s2) {
44 | return s + "-" + s2;
45 | }
46 | });
47 | }
48 |
49 | private Observable zipWithIterableObserver() {
50 | return Observable.zip(createObserver(2),
51 | createObserver(3), createObserver(4),
52 | new Func3() {
53 | @Override
54 | public String call(String s, String s2, String s3) {
55 | return s + "-" + s2 + "-" + s3;
56 | }
57 | });
58 | }
59 |
60 | private Observable createObserver(int index) {
61 | return Observable.interval(100, TimeUnit.MILLISECONDS).take(index)
62 | .map(new Func1() {
63 | @Override
64 | public String call(Long aLong) {
65 | return index + ":" + aLong;
66 | }
67 | });
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/combining/CombineLatestActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.combining;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.concurrent.TimeUnit;
8 |
9 | import cn.com.chaoba.rxjavademo.BaseActivity;
10 | import rx.Observable;
11 | import rx.functions.Action1;
12 | import rx.functions.Func2;
13 | import rx.functions.FuncN;
14 |
15 | public class CombineLatestActivity extends BaseActivity {
16 | List> list = new ArrayList<>();
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | mLButton.setText("combineList");
22 | mLButton.setOnClickListener(e -> {
23 | list.clear();
24 | combineListObserver().subscribe(new Action1() {
25 | @Override
26 | public void call(String i) {
27 | log("combineList:" + i);
28 | }
29 | });
30 | });
31 | mRButton.setText("CombineLatest");
32 | mRButton.setOnClickListener(e -> {
33 | combineLatestObserver().subscribe(new Action1() {
34 | @Override
35 | public void call(String i) {
36 | log("CombineLatest" + i);
37 | }
38 | });
39 | });
40 | }
41 |
42 | private Observable createObserver(int index) {
43 | return Observable.interval(500 * index, TimeUnit.MILLISECONDS);
44 | }
45 |
46 | private Observable combineLatestObserver() {
47 | return Observable.combineLatest(createObserver(1), createObserver(2),
48 | new Func2() {
49 | @Override
50 | public String call(Long num1, Long num2) {
51 | return ("left:" + num1 + " right:" + num2);
52 | }
53 | });
54 | }
55 |
56 |
57 | private Observable combineListObserver() {
58 | for (int i = 1; i < 3; i++) {
59 | list.add(createObserver(i));
60 | }
61 | return Observable.combineLatest(list, new FuncN() {
62 | @Override
63 | public String call(Object... args) {
64 | String temp = "";
65 | for (Object i : args) {
66 | temp = temp + ":" + i;
67 | }
68 | return temp;
69 | }
70 | });
71 | }
72 |
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/SingleActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Single;
8 | import rx.SingleSubscriber;
9 |
10 | public class SingleActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("Single data");
16 | mLButton.setOnClickListener(new View.OnClickListener() {
17 | @Override
18 | public void onClick(View v) {
19 | Single.create(new Single.OnSubscribe() {
20 | @Override
21 | public void call(SingleSubscriber super Integer> singleSubscriber) {
22 | if (!singleSubscriber.isUnsubscribed()) {
23 | singleSubscriber.onSuccess(1);
24 | }
25 | }
26 | }).subscribe(new SingleSubscriber() {
27 | @Override
28 | public void onSuccess(Integer value) {
29 | log(value);
30 | }
31 |
32 | @Override
33 | public void onError(Throwable error) {
34 | log(error.getMessage());
35 | }
36 | });
37 | }
38 | });
39 | mRButton.setText("Single error");
40 | mRButton.setOnClickListener(new View.OnClickListener() {
41 | @Override
42 | public void onClick(View v) {
43 | Single.create(new Single.OnSubscribe() {
44 | @Override
45 | public void call(SingleSubscriber super Integer> singleSubscriber) {
46 | if (!singleSubscriber.isUnsubscribed()) {
47 | singleSubscriber.onError(new Throwable("Single error"));
48 | }
49 | }
50 | }).subscribe(new SingleSubscriber() {
51 | @Override
52 | public void onSuccess(Integer value) {
53 | log(value);
54 | }
55 |
56 | @Override
57 | public void onError(Throwable error) {
58 | log(error.getMessage());
59 | }
60 | });
61 | }
62 | });
63 | }
64 |
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/transforming/WindowActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.transforming;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import rx.Observable;
10 | import rx.functions.Action1;
11 |
12 | public class WindowActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | mLButton.setText("window");
18 | mLButton.setOnClickListener(new View.OnClickListener() {
19 | @Override
20 | public void onClick(View v) {
21 | windowCountObserver()
22 | .subscribe(new Action1>() {
23 | @Override
24 | public void call(Observable i) {
25 | log(i.getClass().getName());
26 | i.subscribe(new Action1() {
27 | @Override
28 | public void call(Integer j) {
29 | log("window:" + j);
30 | }
31 | });
32 | }
33 | });
34 | }
35 | });
36 | mRButton.setText("Time");
37 | mRButton.setOnClickListener(new View.OnClickListener() {
38 | @Override
39 | public void onClick(View v) {
40 | windowTimeObserver().subscribe(new Action1>() {
41 | @Override
42 | public void call(Observable i) {
43 | log(System.currentTimeMillis() / 1000);
44 | i.subscribe(new Action1() {
45 | @Override
46 | public void call(Long j) {
47 | log("windowTime:" + j);
48 | }
49 | });
50 | }
51 | });
52 | }
53 | });
54 | }
55 |
56 | private Observable> windowCountObserver() {
57 | return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).window(3);
58 | }
59 |
60 | private Observable> windowTimeObserver() {
61 | return Observable.interval(1000, TimeUnit.MILLISECONDS)
62 | .window(3000, TimeUnit.MILLISECONDS);
63 | }
64 |
65 |
66 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/transforming/FlatMapActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.transforming;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.ArrayList;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import cn.com.chaoba.rxjavademo.R;
10 | import rx.Observable;
11 | import rx.functions.Action1;
12 | import rx.functions.Func1;
13 |
14 | public class FlatMapActivity extends BaseActivity {
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | mLButton.setText("flatMap");
20 | mLButton.setOnClickListener(new View.OnClickListener() {
21 | @Override
22 | public void onClick(View v) {
23 | flatMapObserver().subscribe(new Action1() {
24 | @Override
25 | public void call(String s) {
26 | log(s);
27 | }
28 | });
29 | }
30 | });
31 | mRButton.setText("flatMapIterable");
32 | mRButton.setOnClickListener(new View.OnClickListener() {
33 | @Override
34 | public void onClick(View v) {
35 | flatMapIterableObserver().subscribe(new Action1() {
36 | @Override
37 | public void call(String s) {
38 | log(s);
39 | }
40 | });
41 | }
42 | });
43 | }
44 |
45 | private Observable flatMapObserver() {
46 | return Observable.just(1, 2, 3)
47 | .flatMap(new Func1>() {
48 | @Override
49 | public Observable call(Integer integer) {
50 | return Observable.just("flat map:" + integer);
51 | }
52 | });
53 | }
54 |
55 | private Observable flatMapIterableObserver() {
56 | return Observable.just(1, 2, 3)
57 | .flatMapIterable(
58 | new Func1>() {
59 | @Override
60 | public Iterable call(Integer integer) {
61 | ArrayList s = new ArrayList<>();
62 | for (int i = 0; i < 3; i++) {
63 | s.add("flatMapIterable:" + integer);
64 | }
65 | return s;
66 | }
67 | }
68 | );
69 | }
70 |
71 |
72 | }
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/conditional_boolean/AllAndAmbActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.conditional_boolean;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.functions.Action1;
10 | import rx.functions.Func1;
11 |
12 | public class AllAndAmbActivity extends BaseActivity {
13 | boolean tag;
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | mLButton.setText("all");
19 | mLButton.setOnClickListener(e -> {
20 | allObserver().subscribe(new Action1() {
21 | @Override
22 | public void call(Boolean aBoolean) {
23 | log("all:" + aBoolean);
24 | }
25 | });
26 | notAllObserver().subscribe(new Action1() {
27 | @Override
28 | public void call(Boolean aBoolean) {
29 | log("not all:" + aBoolean);
30 | }
31 | });
32 | });
33 | mRButton.setText("amb");
34 | mRButton.setOnClickListener(e -> {
35 | ambObserver().subscribe(new Action1() {
36 | @Override
37 | public void call(Integer i) {
38 | log("amb:" + i);
39 | }
40 | });
41 | });
42 | }
43 |
44 | private Observable allObserver() {
45 | Observable just = Observable.just(1, 2, 3, 4, 5);
46 | return just.all(new Func1() {
47 | @Override
48 | public Boolean call(Integer integer) {
49 | return integer < 6;
50 | }
51 | });
52 | }
53 |
54 | private Observable notAllObserver() {
55 | Observable just = Observable.just(1, 2, 3, 4, 5, 6);
56 | return just.all(new Func1() {
57 | @Override
58 | public Boolean call(Integer integer) {
59 | return integer < 6;
60 | }
61 | });
62 | }
63 |
64 | private Observable ambObserver() {
65 | Observable delay3 = Observable.just(1, 2, 3)
66 | .delay(3000, TimeUnit.MILLISECONDS);
67 | Observable delay2 = Observable.just(4, 5, 6)
68 | .delay(2000, TimeUnit.MILLISECONDS);
69 | Observable delay1 = Observable.just(7, 8, 9)
70 | .delay(1000, TimeUnit.MILLISECONDS);
71 | return Observable.amb(delay1, delay2, delay3);
72 | }
73 | }
74 |
75 |
76 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/combining/MergeActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.combining;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.Subscriber;
8 | import rx.functions.Action1;
9 |
10 | public class MergeActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("Merge");
16 | mLButton.setOnClickListener(e -> {
17 | mergeObserver().subscribe(new Action1() {
18 | @Override
19 | public void call(Integer i) {
20 | log("Merge:" + i);
21 | }
22 | });
23 | });
24 | mRButton.setText("mergeDelayError");
25 | mRButton.setOnClickListener(e -> {
26 | mergeDelayErrorObserver().subscribe(new Subscriber() {
27 | @Override
28 | public void onCompleted() {
29 | log("onCompleted");
30 | }
31 |
32 | @Override
33 | public void onError(Throwable e) {
34 | log("mergeDelayError:" + e);
35 |
36 | }
37 |
38 | @Override
39 | public void onNext(Integer integer) {
40 | log("mergeDelayError:" + integer);
41 |
42 | }
43 | });
44 | });
45 | }
46 |
47 | private Observable mergeObserver() {
48 | return Observable.merge(Observable.just(1, 2, 3), Observable.just(4, 5, 6));
49 | }
50 |
51 | private Observable mergeDelayErrorObserver() {
52 | return Observable.mergeDelayError(Observable
53 | .create(new Observable.OnSubscribe() {
54 | @Override
55 | public void call(Subscriber super Integer> subscriber) {
56 | for (int i = 0; i < 5; i++) {
57 | if (i == 3) {
58 | subscriber.onError(new Throwable("error"));
59 | }
60 | subscriber.onNext(i);
61 |
62 | }
63 | }
64 | }), Observable.create(new Observable.OnSubscribe() {
65 | @Override
66 | public void call(Subscriber super Integer> subscriber) {
67 | for (int i = 0; i < 5; i++) {
68 | subscriber.onNext(5 + i);
69 | }
70 | subscriber.onCompleted();
71 | }
72 | }));
73 | }
74 |
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/utility/UsingActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.utility;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscriber;
10 | import rx.functions.Action1;
11 | import rx.functions.Func0;
12 | import rx.functions.Func1;
13 |
14 | public class UsingActivity extends BaseActivity {
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | Observable observable = usingObservable();
20 | Subscriber subscriber = new Subscriber() {
21 | @Override
22 | public void onCompleted() {
23 | log("onCompleted");
24 | }
25 |
26 | @Override
27 | public void onError(Throwable e) {
28 | log("onError");
29 | }
30 |
31 | @Override
32 | public void onNext(Object o) {
33 | log("onNext" + o);
34 | }
35 | };
36 | mLButton.setText("using");
37 | mLButton.setOnClickListener(e -> {
38 | observable.subscribe(subscriber);
39 | });
40 | mRButton.setText("unSubscrib");
41 | mRButton.setOnClickListener(e -> {
42 | subscriber.unsubscribe();
43 | });
44 | }
45 |
46 | private Observable usingObservable() {
47 | return Observable.using(new Func0() {
48 | @Override
49 | public Animal call() {
50 | return new Animal();
51 | }
52 | }, new Func1>() {
53 | @Override
54 | public Observable extends Long> call(Animal animal) {
55 | return Observable.timer(5000, TimeUnit.MILLISECONDS);
56 | }
57 | }, new Action1() {
58 | @Override
59 | public void call(Animal animal) {
60 | animal.release();
61 | }
62 | });
63 | }
64 |
65 | private class Animal {
66 | Subscriber subscriber = new Subscriber() {
67 | @Override
68 | public void onCompleted() {
69 |
70 | }
71 |
72 | @Override
73 | public void onError(Throwable e) {
74 |
75 | }
76 |
77 | @Override
78 | public void onNext(Object o) {
79 | log("animal eat");
80 | }
81 | };
82 |
83 | public Animal() {
84 | log("create animal");
85 | Observable.interval(1000, TimeUnit.MILLISECONDS)
86 | .subscribe(subscriber);
87 | }
88 |
89 | public void release() {
90 | log("animal released");
91 | subscriber.unsubscribe();
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/errorhandling/OnExceptionActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.errorhandling;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.Subscriber;
8 |
9 | public class OnExceptionActivity extends BaseActivity {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 | mLButton.setText("onException-true");
15 | mLButton.setOnClickListener(e -> {
16 | onExceptionResumeObserver(true).subscribe(new Subscriber() {
17 | @Override
18 | public void onCompleted() {
19 | log("onException-true-onCompleted\n");
20 | }
21 |
22 | @Override
23 | public void onError(Throwable e) {
24 | log("onException-true-onError:" + e.getMessage());
25 | }
26 |
27 | @Override
28 | public void onNext(String s) {
29 | log("onException-true-onNext:" + s);
30 | }
31 | });
32 | });
33 | mRButton.setText("onException-false");
34 | mRButton.setOnClickListener(e -> {
35 | onExceptionResumeObserver(false).subscribe(new Subscriber() {
36 | @Override
37 | public void onCompleted() {
38 | log("onException-false-onCompleted\n");
39 | }
40 |
41 | @Override
42 | public void onError(Throwable e) {
43 | log("onException-false-onError:" + e.getMessage());
44 | }
45 |
46 | @Override
47 | public void onNext(String s) {
48 | log("onException-false-onNext:" + s);
49 | }
50 | });
51 | });
52 | }
53 |
54 | private Observable onExceptionResumeObserver(boolean isException) {
55 | return createObserver(isException)
56 | .onExceptionResumeNext(Observable.just("7", "8", "9"));
57 | }
58 |
59 |
60 | private Observable createObserver(Boolean createExcetion) {
61 | return Observable.create(new Observable.OnSubscribe() {
62 | @Override
63 | public void call(Subscriber super String> subscriber) {
64 | for (int i = 1; i <= 6; i++) {
65 | if (i < 3) {
66 | subscriber.onNext("onNext:" + i);
67 | } else if (createExcetion) {
68 | subscriber.onError(new Exception("Exception"));
69 | } else {
70 | subscriber.onError(new Throwable("Throw error"));
71 |
72 | }
73 | }
74 | }
75 | });
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/creatingobserver/CreateAndRangeActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.creatingobserver;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.Random;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import rx.Observable;
10 | import rx.Subscriber;
11 | import rx.functions.Action1;
12 |
13 | public class CreateAndRangeActivity extends BaseActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | mLButton.setText("Create");
19 | mLButton.setOnClickListener(new View.OnClickListener() {
20 | @Override
21 | public void onClick(View v) {
22 | createObserver().subscribe(new Subscriber() {
23 | @Override
24 | public void onCompleted() {
25 | log("onComplete!");
26 | }
27 |
28 | @Override
29 | public void onError(Throwable e) {
30 | log("onError:" + e.getMessage());
31 | }
32 |
33 | @Override
34 | public void onNext(Integer integer) {
35 | log("onNext:" + integer);
36 | }
37 | });
38 | }
39 | });
40 | mRButton.setText("Range");
41 | mRButton.setOnClickListener(new View.OnClickListener() {
42 | @Override
43 | public void onClick(View v) {
44 | Observable.range(10, 5).subscribe(new Action1() {
45 | @Override
46 | public void call(Integer integer) {
47 | log(integer);
48 | }
49 | });
50 | }
51 | });
52 | }
53 |
54 | private Observable createObserver() {
55 | return Observable.create(new Observable.OnSubscribe() {
56 | @Override
57 | public void call(Subscriber super Integer> subscriber) {
58 | if (!subscriber.isUnsubscribed()) {
59 | for (int i = 0; i < 5; i++) {
60 | int temp = new Random().nextInt(10);
61 | if (temp > 8) {
62 | //if value>8, we make an error
63 | subscriber.onError(new Throwable("value >8"));
64 | break;
65 | } else {
66 | subscriber.onNext(temp);
67 | }
68 | // on error,complete the job
69 | if (i == 4) {
70 | subscriber.onCompleted();
71 | }
72 | }
73 | }
74 | }
75 | });
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/errorhandling/OnErrorActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.errorhandling;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.Subscriber;
8 | import rx.functions.Func1;
9 |
10 | public class OnErrorActivity extends BaseActivity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | mLButton.setText("onErrorReturn");
16 | mLButton.setOnClickListener(e -> {
17 | onErrorReturnObserver().subscribe(new Subscriber() {
18 | @Override
19 | public void onCompleted() {
20 | log("onErrorReturn-onCompleted");
21 | }
22 |
23 | @Override
24 | public void onError(Throwable e) {
25 | log("onErrorReturn-onError:" + e.getMessage());
26 | }
27 |
28 | @Override
29 | public void onNext(String s) {
30 | log("onErrorReturn-onNext:" + s);
31 | }
32 | });
33 | });
34 | mRButton.setText("onErrorResume");
35 | mRButton.setOnClickListener(e -> {
36 | onErrorResumeNextObserver().subscribe(new Subscriber() {
37 | @Override
38 | public void onCompleted() {
39 | log("onErrorResume-onCompleted");
40 | }
41 |
42 | @Override
43 | public void onError(Throwable e) {
44 | log("onErrorResume-onError:" + e.getMessage());
45 | }
46 |
47 | @Override
48 | public void onNext(String s) {
49 | log("onErrorResume-onNext:" + s);
50 | }
51 | });
52 | });
53 | }
54 |
55 | private Observable onErrorReturnObserver() {
56 | return createObserver().onErrorReturn(new Func1() {
57 | @Override
58 | public String call(Throwable throwable) {
59 | return "onErrorReturn";
60 | }
61 | });
62 | }
63 |
64 | private Observable onErrorResumeNextObserver() {
65 | return createObserver().onErrorResumeNext(Observable.just("7", "8", "9"));
66 | }
67 |
68 | private Observable createObserver() {
69 | return Observable.create(new Observable.OnSubscribe() {
70 | @Override
71 | public void call(Subscriber super String> subscriber) {
72 | for (int i = 1; i <= 6; i++) {
73 | if (i < 3) {
74 | subscriber.onNext("onNext:" + i);
75 | } else {
76 | subscriber.onError(new Throwable("Throw error"));
77 | }
78 | }
79 | }
80 | });
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/Filtering/DebounceActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.filtering;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | import cn.com.chaoba.rxjavademo.BaseActivity;
9 | import rx.Observable;
10 | import rx.Subscriber;
11 | import rx.functions.Action1;
12 | import rx.functions.Func1;
13 | import rx.schedulers.Schedulers;
14 |
15 | public class DebounceActivity extends BaseActivity {
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | mLButton.setText("throttleWithTimeout");
21 | mLButton.setOnClickListener(new View.OnClickListener() {
22 | @Override
23 | public void onClick(View v) {
24 | throttleWithTimeoutObserver().subscribe(new Action1() {
25 | @Override
26 | public void call(Integer i) {
27 | log("throttleWithTimeout:" + i);
28 | }
29 | });
30 | }
31 | });
32 |
33 | mRButton.setText("debounce");
34 | mRButton.setOnClickListener(new View.OnClickListener() {
35 | @Override
36 | public void onClick(View v) {
37 | debounceObserver().subscribe(new Action1() {
38 | @Override
39 | public void call(Long i) {
40 | log("debounce:" + i);
41 | }
42 | });
43 | }
44 | });
45 |
46 |
47 | }
48 |
49 | private Observable debounceObserver() {
50 | return Observable.interval(1000, TimeUnit.MILLISECONDS)
51 | .debounce(new Func1>() {
52 | @Override
53 | public Observable call(Long aLong) {
54 | return Observable.timer(aLong % 2 * 1500, TimeUnit.MILLISECONDS);
55 | }
56 | });
57 | }
58 |
59 | private Observable throttleWithTimeoutObserver() {
60 | return createObserver().throttleWithTimeout(200, TimeUnit.MILLISECONDS);
61 |
62 | }
63 |
64 | private Observable createObserver() {
65 | return Observable.create(new Observable.OnSubscribe() {
66 | @Override
67 | public void call(Subscriber super Integer> subscriber) {
68 | for (int i = 0; i < 10; i++) {
69 | if (!subscriber.isUnsubscribed()) {
70 | subscriber.onNext(i);
71 | }
72 | int sleep = 100;
73 | if (i % 3 == 0) {
74 | sleep = 300;
75 | }
76 | try {
77 | Thread.sleep(sleep);
78 | } catch (InterruptedException e) {
79 | e.printStackTrace();
80 | }
81 |
82 | }
83 | subscriber.onCompleted();
84 | }
85 | }).subscribeOn(Schedulers.computation());
86 | }
87 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RxJavaDemo
3 |
4 | Hello world!
5 | Settings
6 | BaseActivity
7 | CreateAndRangeActivity
8 | SingleActivity
9 | CompletableActivity
10 | DeferActivity
11 | FromActivity
12 | IntervalActivity
13 | RepeatActivity
14 | BufferActivity
15 | FlatMapActivity
16 | GroupbyActivity
17 | MapActivity
18 | ScanActivity
19 | WindowActivity
20 | DebounceActivity
21 | DistinctActivity
22 | ElementAtAndFirstActivity
23 | FirstActivity
24 | SkipAndTakeActivity
25 | SampleActivity
26 | CombineLatestActivity
27 | CatchActivity
28 | RetryActivity
29 | DelayActivity
30 | MeterializeActivity
31 | MeterializeActivity
32 | SubscribeOnAndObserveOnActivity
33 | TimeIntervalTimeStampActivity
34 | TimeoutActivity
35 | UsingActivity
36 | AllAndAmbActivity
37 | ContainsActivity
38 | DefaultIfEmptyActivity
39 | SequenceEqualActivity
40 | SkipUntilSkipWhileActivity
41 | TakeUntilTakeWhileActivity
42 | ContactAndCountActivity
43 | Reducectivity
44 | PublishAndConnectActivity
45 | RefCountActivity
46 | ReplayActivity
47 | CustomActivity
48 | FromExecutorActivity
49 |
50 |
51 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/transforming/GroupbyActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.transforming;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import cn.com.chaoba.rxjavademo.BaseActivity;
7 | import rx.Observable;
8 | import rx.functions.Action1;
9 | import rx.functions.Func1;
10 | import rx.observables.GroupedObservable;
11 |
12 | public class GroupbyActivity extends BaseActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | mLButton.setText("groupBy");
18 | mLButton.setOnClickListener(new View.OnClickListener() {
19 | @Override
20 | public void onClick(View v) {
21 | groupByObserver().subscribe(new Action1>() {
22 | @Override
23 | public void call(GroupedObservable groupedObservable) {
24 | groupedObservable.count().subscribe(new Action1() {
25 | @Override
26 | public void call(Integer integer) {
27 | log("key" + groupedObservable.getKey()
28 | + " contains:" + integer + " numbers");
29 | }
30 | });
31 | }
32 | });
33 | }
34 | });
35 | mRButton.setText("groupByKeyValue");
36 | mRButton.setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | groupByStringObserver().subscribe(new Action1>() {
40 | @Override
41 | public void call(GroupedObservable groupedObservable) {
42 | if (groupedObservable.getKey() == 0) {
43 | groupedObservable.subscribe(new Action1() {
44 | @Override
45 | public void call(String s) {
46 | log(s);
47 | }
48 | });
49 | }
50 | }
51 | });
52 | }
53 | });
54 | }
55 |
56 | private Observable> groupByObserver() {
57 | return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
58 | .groupBy(new Func1() {
59 | @Override
60 | public Integer call(Integer integer) {
61 | return integer % 2;
62 | }
63 | });
64 | }
65 |
66 | private Observable> groupByStringObserver() {
67 | return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
68 | .groupBy(new Func1() {
69 | @Override
70 | public Integer call(Integer integer) {
71 | return integer % 2;
72 | }
73 | }, new Func1() {
74 | @Override
75 | public String call(Integer integer) {
76 | return "groupByKeyValue:" + integer;
77 | }
78 | });
79 | }
80 | }
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/custom/CustomActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.custom;
2 |
3 | import android.os.Bundle;
4 |
5 | import cn.com.chaoba.rxjavademo.BaseActivity;
6 | import rx.Observable;
7 | import rx.Observable.Operator;
8 | import rx.Observable.Transformer;
9 | import rx.Subscriber;
10 | import rx.functions.Action1;
11 | import rx.functions.Func1;
12 |
13 | public class CustomActivity extends BaseActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | mLButton.setText("lift");
19 | mLButton.setOnClickListener(e -> {
20 | liftObserver().subscribe(new Action1() {
21 | @Override
22 | public void call(String s) {
23 | log("lift:" + s);
24 | }
25 | });
26 | });
27 | mRButton.setText("compose");
28 | mRButton.setOnClickListener(e -> {
29 | composeObserver().subscribe(new Action1() {
30 | @Override
31 | public void call(String s) {
32 | log("compose:" + s);
33 | }
34 | });
35 | });
36 | }
37 |
38 | private Observable liftObserver() {
39 | Operator myOperator = new Operator() {
40 | @Override
41 | public Subscriber super String> call(
42 | Subscriber super String> subscriber) {
43 | return new Subscriber(subscriber) {
44 | @Override
45 | public void onCompleted() {
46 | if (!subscriber.isUnsubscribed()) {
47 | subscriber.onCompleted();
48 | }
49 | }
50 |
51 | @Override
52 | public void onError(Throwable e) {
53 | if (!subscriber.isUnsubscribed()) {
54 | subscriber.onError(e);
55 | }
56 | }
57 |
58 | @Override
59 | public void onNext(String s) {
60 | if (!subscriber.isUnsubscribed()) {
61 | subscriber.onNext("myOperator:" + s);
62 | }
63 | }
64 | };
65 | }
66 |
67 | };
68 | return Observable.just(1, 2, 3)
69 | .map(new Func1() {
70 | @Override
71 | public String call(Integer integer) {
72 | return "map1:" + integer;
73 | }
74 | })
75 | .lift(myOperator)
76 | .map(new Func1() {
77 | @Override
78 | public String call(String integer) {
79 | return "map2:" + integer;
80 | }
81 | });
82 | }
83 |
84 | private Observable composeObserver() {
85 | Transformer myTransformer =
86 | new Transformer() {
87 | @Override
88 | public Observable call(
89 | Observable integerObservable) {
90 | return integerObservable
91 | .map(integer -> "myTransforer:" + integer)
92 | .doOnNext(s -> log("doOnNext:" + s));
93 | }
94 | };
95 | return Observable.just(1, 2, 3).compose(myTransformer);
96 | }
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/com/chaoba/rxjavademo/errorhandling/RetryActivity.java:
--------------------------------------------------------------------------------
1 | package cn.com.chaoba.rxjavademo.errorhandling;
2 |
3 | import android.os.Bundle;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import cn.com.chaoba.rxjavademo.BaseActivity;
8 | import rx.Observable;
9 | import rx.Subscriber;
10 | import rx.functions.Func1;
11 | import rx.functions.Func2;
12 |
13 | public class RetryActivity extends BaseActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | mLButton.setText("retry");
19 | mLButton.setOnClickListener(e -> {
20 | retryObserver().subscribe(new Subscriber