每天學習一個Android中的常用框架——15.RxJava

1.簡介

RxJava,是一個開發庫、是一種代碼風格、也是一種思維方式。爲了讓Rxjava適配到Android平臺上,RxAndroid應運而生。RxJava相當熱門,在GItHub的官方網站:Rxjava上已擁有42k的Star數,可見其受歡迎的程度。

RxJava的特點是簡潔、優雅、高效,它的優點是多線程切換簡單、數據變換容易、代碼簡潔可讀性好、第三方支持豐富易於開發;缺點是學習成本較高、出錯難以排查。

怎麼理解這個學習成本較高呢?說實話,要想正確使用RxJava,需要學習其中的很多概念,思想,很容易勸退無法理解的學習者(傳說中的上手難)。而RxJava又經歷從1.0、2.0、3.0這三個大型版本的迭代,其中更新的api數不勝數,讓RxJava的學習變得越發艱難。

RxJava擁有很多個比較鮮明的特點:響應式編程鏈式編程觀察者模式等。若想深究這些特點需要耗費相當多的時間,所以這裏就不再贅述。鑑於作者的水平有限,這裏僅展示RxJava的基本功能:即異步數據交互,更多的功能,可以參考下一小節列出的博客。

2.特性

爲了不誤人子弟,這裏就不再描述RxJava的一些特性,而是列舉出幾篇作者在學習RxJava中寫的比較好的博文,建議按照順序閱讀:

  1. RxAndroid使用初探—簡潔、優雅、高效:這篇博文寫的比較容易令人理解,閱讀完後基本上就能瞭解RxJava的基本特性和使用
  2. 給 Android 開發者的 RxJava 詳解:這篇博文介紹了RxJava的運作機制,對於瞭解RxJava的運作大有幫助
  3. 大佬們,一波RxJava 3.0來襲,請做好準備~:這邊博文將RxJava3.0的新特性寫的相當詳細,對於想要深入瞭解的讀者相當有幫助

關於RxJava的應用有很多,也有很多比較方便配合RxJava的框架,如RxPermissionRxLifeCycleRxCacheRxBus等等,有興趣的讀者可以查詢相關資料,這裏不再贅述。

3.演示

3.1 集成

在使用RxJava前,我們需要先導入相應依賴,由於要使用在Android平臺上,所以還需要導入RxAndroid的依賴。與此同時,若在Android O版本以上使用RxJava,還需要導入compileOptions閉包,聲明JDK版本,整體的module下的build.gradle代碼如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.androidframelearn.event_rxjava"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    // 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.
    // (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
    implementation 'io.reactivex.rxjava3:rxjava:3.0.0'

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

根據官方的建議,最好在整體項目的build.gradle下添加RxJava的Maven倉庫,這樣下載會更快一些,代碼如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
        mavenCentral() // add repository
        maven { url "https://oss.jfrog.org/libs-snapshot" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral() // add repository
        maven { url "https://oss.jfrog.org/libs-snapshot" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

3.2 佈局文件

這裏想要演示一個簡單的功能:點擊按鈕後改變文本,只需要在佈局文件上添加一個按鈕和一個文本控件,修改activity_main.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_run_scheduler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開始訂閱"/>

    <TextView
        android:id="@+id/tv_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="測試文本"
        android:textSize="100sp"/>

</LinearLayout>

3.3 代碼實現

修改MainActivity,在相應的代碼塊上均有詳細的註釋,代碼如下:

package com.androidframelearn.event_rxjava;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button btn_run_scheduler;

    private TextView tv_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 0.獲取控件實例
        btn_run_scheduler = findViewById(R.id.btn_run_scheduler);
        tv_test = findViewById(R.id.tv_test);

        // 1.創建被觀察者
        Observable observable = Observable.create(new ObservableOnSubscribe<String>() {
            // 2.實現回調方法subscribe()
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
                // 3.發送String類型的消息
                emitter.onNext("測試RxJava!");
                // 4.完成消息的發送
                emitter.onComplete();
            }
        });

        // 5.創建觀察着
        Observer observer = new Observer<String>() {
            // 6.訂閱後執行的方法
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            // 7.當被觀察者執行onNext()的回調
            @Override
            public void onNext(@NonNull String s) {
                tv_test.setText(s);
                Log.i(TAG,"收到消息啦!");
            }

            // 8.當被觀察者執行onError()的回調
            @Override
            public void onError(@NonNull Throwable e) {

            }

            // 9.當被觀察者執行onComplete()的回調
            @Override
            public void onComplete() {
                Log.i(TAG,"消息接收已結束!");
            }
        };

        // 10.註冊按鈕點擊事件
        btn_run_scheduler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                observable.subscribe(observer);
            }
        });
    }
}

4.源碼地址

AFL——Android框架學習

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章