-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 74ce657
Showing
86 changed files
with
5,618 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
.idea | ||
/captures | ||
.externalNativeBuild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
# RxUtil2 | ||
[![RxUtil2][rxSvg]][rx] [![api][apiSvg]][api] | ||
|
||
一个实用的RxJava2工具类库。 | ||
|
||
> 如果你习惯RxJava1,请移步[RxUtil](https://github.com/xuexiangjys/RxUtil) | ||
## 关于我 | ||
[](https://github.com/xuexiangjys) [](http://blog.csdn.net/xuexiangjys) | ||
|
||
## 内容 | ||
- RxBus 支持多事件定义,支持数据携带,支持全局和局部的事件订阅和注销 | ||
- 订阅池管理 | ||
- 线程调度辅助工具 | ||
- RxBinding 使用工具类 | ||
- RxJava常用方法工具类 | ||
|
||
## 1、演示(请star支持) | ||
|
||
### 1.1、RxBus | ||
 | ||
|
||
|
||
## 2、如何使用 | ||
目前支持主流开发工具AndroidStudio的使用,直接配置build.gradle,增加依赖即可. | ||
|
||
### 2.1、Android Studio导入方法,添加Gradle依赖 | ||
|
||
先在项目根目录的 build.gradle 的 repositories 添加: | ||
``` | ||
allprojects { | ||
repositories { | ||
... | ||
maven { url "https://jitpack.io" } | ||
} | ||
} | ||
``` | ||
|
||
然后在dependencies添加: | ||
|
||
``` | ||
dependencies { | ||
... | ||
implementation 'com.github.xuexiangjys:RxUtil2:1.0' | ||
} | ||
``` | ||
### 3.1、RxBus使用 | ||
|
||
#### 3.1.1、事件注册订阅 | ||
|
||
1.使用RxBusUtils.get().onMainThread方法注册事件,并指定订阅发生在主线程。 | ||
|
||
``` | ||
RxBusUtils.get().onMainThread(EventKey.EVENT_HAVE_DATA, Event.class, new Consumer<Event>() { | ||
@Override | ||
public void accept(Event event) throws Exception { | ||
showContent(EventKey.EVENT_HAVE_DATA, event.toString()); | ||
} | ||
}); | ||
``` | ||
2.使用RxBusUtils.get().on方法注册事件,订阅所在线程为事件发生线程,也可指定订阅发生的线程。 | ||
|
||
``` | ||
RxBusUtils.get().on(EventKey.EVENT_BACK_NORMAL, String.class, new Consumer<String>() { | ||
@Override | ||
public void accept(String eventName) throws Exception { | ||
final String msg = "事件Key:" + EventKey.EVENT_BACK_NORMAL + "\n EventName:" + eventName + ", 当前线程状态: " + Event.getLooperStatus(); | ||
showContent(msg); | ||
} | ||
}); | ||
``` | ||
|
||
3.与RxBus1相比,使用RxJava2的RxBus2需要指定接收数据的类型,但如果使用默认的RxEvent进行事件注册, 就不需要指定类型了。 | ||
|
||
``` | ||
RxBusUtils.get().onMainThread(EventKey.EVENT_NO_DATA, new Consumer<RxEvent>() { | ||
@Override | ||
public void accept(RxEvent rxEvent) throws Exception { | ||
showContent(rxEvent.toString()); | ||
} | ||
}); | ||
``` | ||
|
||
|
||
#### 3.1.2、事件发送 | ||
|
||
1.使用RxBusUtils.get().post(Object eventName)发送不带数据的事件。 | ||
``` | ||
RxBusUtils.get().post(EventKey.EVENT_NO_DATA); | ||
RxBusUtils.get().postRxEvent(EventKey.EVENT_NO_DATA); //发送使用RxEvent注册的事件 | ||
``` | ||
|
||
2.使用RxBusUtils.get().post(Object eventName, Object content)发送携带数据的事件。 | ||
``` | ||
RxBusUtils.get().post(EventKey.EVENT_HAVE_DATA, new Event(EventKey.EVENT_HAVE_DATA, "这里携带的是数据")); | ||
RxBusUtils.get().post(EventKey.EVENT_HAVE_DATA, true); | ||
``` | ||
|
||
#### 3.1.3、事件注销 | ||
|
||
1.使用RxBusUtils.get().unregisterAll(Object eventName)取消事件的所有订阅并注销事件。 | ||
``` | ||
RxBusUtils.get().unregisterAll(EventKey.EVENT_HAVE_DATA); | ||
``` | ||
|
||
2.使用RxBusUtils.get().unregister(Object eventName, SubscribeInfo subscribeInfo)取消事件的某个指定订阅。 | ||
SubscribeInfo是事件注册订阅后返回的订阅信息。如果在取消该订阅后,该事件如无其他订阅,便自动注销该事件。 | ||
``` | ||
RxBusUtils.get().unregister(EventKey.EVENT_CLEAR, mSubscribeInfo); | ||
``` | ||
|
||
### 3.2、RxJavaUtils使用 | ||
|
||
#### 3.2.1、线程任务 | ||
|
||
1.RxIOTask:在io线程中操作的任务 | ||
``` | ||
RxJavaUtils.doInIOThread(new RxIOTask<String>("我是入参123") { | ||
@Override | ||
public Void doInIOThread(String s) { | ||
Log.e(TAG, "[doInIOThread] " + getLooperStatus() + ", 入参:" + s); | ||
return null; | ||
} | ||
}); | ||
``` | ||
|
||
2.RxUITask:在UI线程中操作的任务 | ||
|
||
``` | ||
RxJavaUtils.doInUIThread(new RxUITask<String>("我是入参456") { | ||
@Override | ||
public void doInUIThread(String s) { | ||
Log.e(TAG, "[doInUIThread] " + getLooperStatus() + ", 入参:" + s); | ||
} | ||
}); | ||
``` | ||
|
||
3.RxAsyncTask:在IO线程中执行耗时操作 执行完成后在UI线程中订阅的任务。 | ||
``` | ||
RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, Integer>("我是入参789") { | ||
@Override | ||
public Integer doInIOThread(String s) { | ||
Log.e(TAG, "[doInIOThread] " + getLooperStatus() + ", 入参:" + s); | ||
return 12345; | ||
} | ||
@Override | ||
public void doInUIThread(Integer integer) { | ||
Log.e(TAG, "[doInUIThread] " + getLooperStatus() + ", 入参:" + integer); | ||
} | ||
}); | ||
``` | ||
|
||
4.RxIteratorTask:遍历集合或者数组的任务,在IO线程中执行耗时操作 执行完成后在UI线程中订阅的任务。 | ||
``` | ||
RxJavaUtils.executeRxIteratorTask(new RxIteratorTask<String, Integer>(new String[]{"123", "456", "789"}) { | ||
@Override | ||
public Integer doInIOThread(String s) { | ||
RxLog.e("[doInIOThread]" + getLooperStatus() + ", 入参:" + s); | ||
return Integer.parseInt(s); | ||
} | ||
@Override | ||
public void doInUIThread(Integer integer) { | ||
RxLog.e("[doInUIThread] " + getLooperStatus() + ", 入参:" + integer); | ||
} | ||
}); | ||
``` | ||
|
||
#### 3.2.2、订阅者Subscriber | ||
|
||
1.SimpleSubscriber:简单的订阅者,已对错误进行捕获处理,并对生命周期进行日志记录。可设置IExceptionHandler接口自定义错误处理,设置ILogger接口自定义日志记录。 | ||
|
||
2.ProgressLoadingSubscriber:带进度条加载的订阅者,实现IProgressLoader接口可自定义加载方式。 | ||
``` | ||
Observable.just("加载完毕!") | ||
.delay(3, TimeUnit.SECONDS) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribe(new ProgressLoadingSubscriber<String>(mProgressLoader) { | ||
@Override | ||
public void onNext(String s) { | ||
Toast.makeText(RxJavaActivity.this, s, Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
``` | ||
|
||
### 3.3、DisposablePool使用 | ||
|
||
DisposablePool:RxJava的订阅池 | ||
|
||
1.增加订阅:add(@NonNull Object tagName, Subscription m) 或者 add(Subscription m, @NonNull Object tagName) | ||
``` | ||
DisposablePool.get().add(RxJavaUtils.polling(5, new Consumer<Long>() { | ||
@Override | ||
public void accept(Long o) throws Exception { | ||
Toast.makeText(RxJavaActivity.this, "正在监听", Toast.LENGTH_SHORT).show(); | ||
} | ||
}), "polling"); | ||
``` | ||
|
||
2.取消订阅:remove(@NonNull Object tagName)、remove(@NonNull Object tagName, Subscription m)、removeAll() | ||
``` | ||
DisposablePool.get().remove("polling"); | ||
``` | ||
|
||
### 3.4、RxBindingUtils使用 | ||
|
||
1.setViewClicks:设置点击事件 | ||
``` | ||
RxBindingUtils.setViewClicks(mBtnClick, 5, TimeUnit.SECONDS, new Action1<Object>() { | ||
@Override | ||
public void call(Object o) { | ||
toast("触发点击"); | ||
} | ||
}); | ||
``` | ||
|
||
2.setItemClicks:设置条目点击事件 | ||
|
||
|
||
### 3.5、RxSchedulerUtils使用 | ||
|
||
1. 订阅发生在主线程 ( -> -> main) | ||
|
||
``` | ||
.compose(RxSchedulerUtils.<T>_main_o()) //Observable使用 | ||
.compose(RxSchedulerUtils.<T>_main_f()) //Flowable使用 | ||
``` | ||
|
||
2. 订阅发生在io线程 ( -> -> io) | ||
|
||
``` | ||
.compose(RxSchedulerUtils.<T>_io_o()) //Observable使用 | ||
.compose(RxSchedulerUtils.<T>_io_f()) //Flowable使用 | ||
``` | ||
|
||
3. 处理在io线程,订阅发生在主线程( -> io -> main) | ||
|
||
``` | ||
.compose(RxSchedulerUtils.<T>_io_main_o()) //Observable使用 | ||
.compose(RxSchedulerUtils.<T>_io_main_f()) //Flowable使用 | ||
``` | ||
|
||
4. 处理在io线程,订阅也发生在io线程( -> io -> io) | ||
|
||
``` | ||
.compose(RxSchedulerUtils.<T>_io_io_o()) //Observable使用 | ||
.compose(RxSchedulerUtils.<T>_io_io_f()) //Flowable使用 | ||
``` | ||
|
||
|
||
[rxSvg]: https://img.shields.io/badge/RxUtil2-v1.0-brightgreen.svg | ||
[rx]: https://github.com/xuexiangjys/RxUtil2 | ||
[apiSvg]: https://img.shields.io/badge/API-14+-brightgreen.svg | ||
[api]: https://android-arsenal.com/api?level=14 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 26 | ||
defaultConfig { | ||
applicationId "com.xuexiang.rxutil2demo" | ||
minSdkVersion 19 | ||
targetSdkVersion 26 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(include: ['*.jar'], dir: 'libs') | ||
implementation 'com.android.support:appcompat-v7:26.1.0' | ||
//butterknife的sdk | ||
compile 'com.jakewharton:butterknife:8.8.1' | ||
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' | ||
implementation project(':rxutil2') | ||
|
||
//leak | ||
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4' | ||
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' | ||
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.xuexiang.rxutil2demo"> | ||
|
||
<application | ||
android:name="com.xuexiang.rxutil2demo.App" | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".activity.MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
<activity android:name=".activity.RxBusActivity" /> | ||
<activity android:name=".activity.RxJavaActivity"/> | ||
<activity android:name=".activity.RxBindingActivity"/> | ||
|
||
</application> | ||
|
||
</manifest> |
Oops, something went wrong.