版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaihaohao1/article/details/83446578
1、在build.gradle中添加依赖
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
这个依赖包中包含了
rxbinding-0.4.0 (操作控件)
rxjava-1.1.0 (Java相关的)
rxandroid-1.1.0 (安卓相关的)
2具体代码:
2.1MainActivity中
package com.zhh.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.orhanobut.logger.Logger;
import java.util.concurrent.Callable;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
/**
* 第二节
* 常用操作符
*/
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initOnclick();
}
/**
* 初始化控件
*/
private void initView() {
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
}
/**
* 点击事件
*/
private void initOnclick() {
// create
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 被观察者,发消息
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
// 消息是一条一条发送的
subscriber.onNext("我");
subscriber.onNext("是");
subscriber.onNext("翟");
subscriber.onNext("浩浩");
subscriber.onCompleted();
}
});
// 观察者接收消息
Observer<String> observer = new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String value) {
// 接收消息
// 消息是一条一条接收的
Logger.t("111").d(value);
// 打印出来的数据
// 我
// 是
// 翟
// 浩浩
}
};
// 订阅(发送)
observable.subscribe(observer);
}
});
// just
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 发消息
Observable<String> observable = Observable.just("我", "是", "张", "飞");
// 收消息
observable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String message) {
Logger.t("111").d(">>>"+message);
}
});
}
});
// from
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer[] items = { 0, 1, 2, 3, 4, 5 };
// 发消息
Observable<Integer> observable = Observable.from(items);
// 收消息
observable.subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer integer) {
Logger.t("111").d(">>>"+integer);
}
});
}
});
// fromCallable
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 发消息
Observable<String> observable = Observable.fromCallable(new Callable<String>() {
@Override
public String call() throws Exception {
return "我是张飞";
}
});
// 收消息
observable.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
Logger.t("111").d(">>>"+s);
}
});
}
});
}
}
2.2 activity_main.xml 中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮4" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1" />
</LinearLayout>
RxBinding地址
https://github.com/JakeWharton/RxBinding
RxJava中文文档(PDF/ePub/Mobi格式下载 会全面一点)
https://mcxiaoke.gitbooks.io/rxdocs/content/
RxJava英文文档
http://reactivex.io/documentation/operators.html#alphabetical
参考视频:
https://www.cniao5.com/course/lessons/10120
源码下载:
https://download.csdn.net/download/zhaihaohao1/10748103
源码----RxJavaCN2----app2