diff --git a/README.md b/README.md index b666b62..1fa9c19 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,7 @@ # Learning RxJava(RxJava详解) +**为什么做这个项目:** 学习Rxjava,基本所有人都会看扔物线的《给Android开发者Rxjava的详解》,可以说是国内最理想的Rxjava教程了。这片文章后半部分讲对线程的操作,相信很多人都不太懂。这个项目通过最简单的方式,对Rxjava线程操作的进行讲解,让大家彻底明白Rxjava的核心内容。所以,在学习这个项目前先看扔物线大哥的文章。 -RxJava的精妙之处就是异步操作极其方便,也就是Schedulers的使用,本项目可以让大家彻底掌握Schedulers的使用,还有常用的操作符、RxAndroid...的使用详解。 - - - - - -## Screenshots - - - - - +RxJava的精妙之处就是异步操作极其方便,也就是Schedulers的使用,本项目可以让大家彻底掌握Schedulers的使用,还有常用的操作符、RxAndroid...的使用详解。就是用最简单移动的方式把Rxjava讲明白. ## 详解 (Details Explain) @@ -37,59 +27,53 @@ RxJava的精妙之处就是异步操作极其方便,也就是Schedulers的使 ` 其实我们常用的就2种:Schedulers.io()和AndroidSchedulers.mainThread()` - **Note:** Schedulers.io() and AndroidSchedulers.mainThread() are the most common one - > 以下几个例子都是使用Observable.just(1,2,3)创建被观察对象,观察者输出1,2,3. > This example show how to use make a few threads that we can watch their actions -####1. 基本使用 - - +#### 1. 基本使用 + + -####2. 使用 subscribeOn(Schedulers.io())设置被观察者的线程 - - + +#### 2. 使用 subscribeOn(Schedulers.io())设置被观察者的线程 + > 以下几个例子中看不出被观察者发生在什么线程,使用Observeble.create()创建被观察者可以看出发生在什么线程,可参看源码中的其它Demo。 -####3. 使用 subscribeOn(Schedulers.io()) 和 observeOn() 设置被观察者和观察者的线程 +#### 3. 使用 subscribeOn(Schedulers.io()) 和 observeOn() 设置被观察者和观察者的线程 - - + -####4. 使用Schedulers.io()指定被观察者产生事件的线程,然后使用Map对数据转换,这里只是在每个数据后面加‘a’。 +#### 4. 使用Schedulers.io()指定被观察者产生事件的线程,然后使用Map对数据转换,这里只是在每个数据后面加‘a’。 - - + -####5. 使用Schedulers.io()指定被观察者产生事件的线程,使用Map对数据转换,在每个数据后面加‘a’,使用AndroidSchedulers.mainThread()切换到主线程,然后使用Map变换,每个数据后加‘b’,输出结果。 +#### 5. 使用Schedulers.io()指定被观察者产生事件的线程,使用Map对数据转换,在每个数据后面加‘a’,使用AndroidSchedulers.mainThread()切换到主线程,然后使用Map变换,每个数据后加‘b’,输出结果。 - - + -####6. 使用Schedulers.io()指定被观察者产生事件的线程,使用Map对数据转换,在每个数据后面加‘a’,使用AndroidSchedulers.mainThread()切换到主线程,然后使用Map变换,每个数据后加‘b’,再用Schedulers.io()切换线程,用Map对数据加‘c’,输出结果。 +#### 6. 使用Schedulers.io()指定被观察者产生事件的线程,使用Map对数据转换,在每个数据后面加‘a’,使用AndroidSchedulers.mainThread()切换到主线程,然后使用Map变换,每个数据后加‘b’,再用Schedulers.io()切换线程,用Map对数据加‘c’,输出结果。 - - + -####7.这个例子不同的是使用了两次subscribeOn()来指定被观察者的线程,最终最上面的subscribeOn()起作用,下面的subscribeOn()不起作用。然后使用Map对数据转换,这里只是在每个数据后面加‘a’,使用Schedulers.io()切换线程,然后使用Map变换,每个数据后加‘b’,再用AndroidSchedulers.mainThread()切换主线程,用Map对数据加‘c’,最后再切换到非主线程,输出结果。 +#### 7.这个例子不同的是使用了两次subscribeOn()来指定被观察者的线程,最终最上面的subscribeOn()起作用,下面的subscribeOn()不起作用。然后使用Map对数据转换,这里只是在每个数据后面加‘a’,使用Schedulers.io()切换线程,然后使用Map变换,每个数据后加‘b’,再用AndroidSchedulers.mainThread()切换主线程,用Map对数据加‘c’,最后再切换到非主线程,输出结果。 - - + >最终发现设置被观察者的线程只有最上面的起作用,下面的不起作用。 + `更多的例子请看源码` -##About me +## About me An android developer in Beijing.Welcome to offer me an [Interview invitation](mailto:maat.xing@gmail.com). If you have any new idea about this project, feel free to [contact me](mailto:maat.xing@gmail.com). :smiley: diff --git a/app/build.gradle b/app/build.gradle index 5ee223d..392faf1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' apply plugin: 'android-apt' android { compileSdkVersion 24 - buildToolsVersion "24.0.3" + buildToolsVersion '25.0.0' defaultConfig { applicationId "com.demo.maat.hello_rxjava" minSdkVersion 16 @@ -20,16 +20,14 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' - compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' - compile 'io.reactivex:rxandroid:1.0.1' // Because RxAndroid releases are few and far between, it is recommended you also // explicitly depend on RxJava's latest version for bug fixes and new features. @@ -41,6 +39,5 @@ dependencies { compile 'com.squareup.retrofit2:converter-gson:+' compile 'com.squareup.retrofit2:adapter-rxjava:+' compile 'com.google.code.gson:gson:+' - compile 'io.reactivex:rxandroid:1.2.1' } diff --git a/app/src/main/java/com/demo/maat/hello_rxjava/MainActivity.java b/app/src/main/java/com/demo/maat/hello_rxjava/MainActivity.java index 92769c4..8970080 100644 --- a/app/src/main/java/com/demo/maat/hello_rxjava/MainActivity.java +++ b/app/src/main/java/com/demo/maat/hello_rxjava/MainActivity.java @@ -13,37 +13,48 @@ public class MainActivity extends AppCompatActivity { + /** + 通过使用 @BindView 来消除 findViewById + 将多个 View 组织到一个列表中,一次性操作它们 + 通过使用 @onClick 为 View 绑定监听,消除 listener 的匿名内部类 + 通过使用资源注解如 @BindColor,来消除资源的查找 + @Nullable 参数可为NULL - @Nullable @BindView(R.id.btn_rxjava) - Button mBtnRxjava; + */ + + // 声明注解 + @Nullable + @BindView(R.id.btn_rxjava) Button mBtnRxjava; + + // 写法也可以类似下面 @BindView(R.id.btn_retrofit) Button mBtnRetrofit; - @BindView(R.id.btn_rxandroid) - Button mBtnRxAndroid; + + @BindView(R.id.btn_rxandroid) Button mBtnRxAndroid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + // 进行绑定 ButterKnife.bind(this); - } + @OnClick({R.id.btn_rxjava, R.id.btn_retrofit, R.id.btn_rxandroid}) - @OnClick({R.id.btn_rxjava, R.id.btn_retrofit,R.id.btn_rxandroid}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_rxjava: - Intent intent=new Intent(MainActivity.this,RxJavaMainActivity.class); + Intent intent = new Intent(this, RxJavaMainActivity.class); startActivity(intent); break; case R.id.btn_retrofit: - Intent retrofit=new Intent(MainActivity.this,RxJavaRetrofitActivity.class); + Intent retrofit = new Intent(this, RxJavaRetrofitActivity.class); startActivity(retrofit); break; case R.id.btn_rxandroid: - Intent rxandroid=new Intent(MainActivity.this,RxAndroidActivity.class); + Intent rxandroid = new Intent(this, RxAndroidActivity.class); startActivity(rxandroid); break; } diff --git a/app/src/main/java/com/demo/maat/hello_rxjava/common/logger/LogFragment.java b/app/src/main/java/com/demo/maat/hello_rxjava/common/logger/LogFragment.java index 69256ff..5265f7d 100644 --- a/app/src/main/java/com/demo/maat/hello_rxjava/common/logger/LogFragment.java +++ b/app/src/main/java/com/demo/maat/hello_rxjava/common/logger/LogFragment.java @@ -98,7 +98,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void afterTextChanged(Editable s) { - mScrollView.fullScroll(ScrollView.FOCUS_DOWN); + //afterTextChanged 在 text发生改变后马上被调用,此时可能text还未被绘制,导致无法滑动到最底部 + //所以不能直接调用,而应该使用post + mScrollView.post(new Runnable(){ + @Override + public void run() { + mScrollView.fullScroll(ScrollView.FOCUS_DOWN); + } + }); } }); return result; diff --git a/build.gradle b/build.gradle index 5a80b7d..aeba529 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.0' + classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index aac7c9b..c45c9e8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,17 +1,20 @@ -# Project-wide Gradle settings. - -# IDE (e.g. Android Studio) users: -# Gradle settings configured through the IDE *will override* -# any settings specified in this file. - +## Project-wide Gradle settings. +# # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html - +# # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx1536m - +# Default value: -Xmx1024m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +# # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true +#Wed Oct 25 10:26:24 CST 2017 +systemProp.http.proxyHost=dev-proxy.oa.com +systemProp.https.proxyPort=8080 +org.gradle.jvmargs=-Xmx1536m +systemProp.https.proxyHost=dev-proxy.oa.com +systemProp.http.proxyPort=8080