Android中RxJava使用2----基本使用
阿新 • • 發佈:2018-12-17
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>
原始碼下載: