LoginSignup
23
24

More than 5 years have passed since last update.

KotlinでRxAndroid1.0を使う

Last updated at Posted at 2015-10-26
  • KotlinでRxAndroidを使ってみたという話。RxAndroidを追加してViewObservableやWidgetObservableを試そうとしたものの、そんなクラスはいないと言われる。。。

公式に書いてあった

WidgetObservable and ViewObservable have been rolled into (and improved in) RxBinding.
  • ということで前のバージョンではRxAndroid本体に含まれていたもののいくつかはExtension化したらしい。Schedulerは本体に含まれているので非同期回りのハンドリングで使いたいだけなら本体だけで足りそう。

試してみる

Extensionを追加

  • build.gradleのdependenciesに追加。Kotlin向けのは最後に-kotlinをつけてねと書いてあるので従う。
build.grade
dependencies {
    ext.rx_android_kotlin_version = '0.3.0'
中略
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'io.reactivex:rxjava:1.0.14'
    compile "com.jakewharton.rxbinding:rxbinding-kotlin:$rx_android_kotlin_version"
    compile "com.jakewharton.rxbinding:rxbinding-support-v4-kotlin:$rx_android_kotlin_version"
    compile "com.jakewharton.rxbinding:rxbinding-appcompat-v7-kotlin:$rx_android_kotlin_version"
}

実装

  • EditTextの入力欄が4つあって、全部何かしら入力されたらSubmitボタンを有効にするというシチュエーションで作ります。
RxSampleFragment.kt
package me.rei_m.kotlinsample.fragments

import android.os.Bundle
import android.support.v7.widget.AppCompatButton
import android.support.v7.widget.AppCompatEditText
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.jakewharton.rxbinding.widget.RxTextView

import rx.Observable

import me.rei_m.kotlinsample.R

public class RxSampleFragment : AbstractFragment() {

    companion object {
        fun newInstance(): RxSampleFragment {
            return RxSampleFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {

        val view = inflater.inflate(R.layout.fragment_rx_sample, container, false)

        // 各入力欄のObservableを取得
        val orderName = RxTextView.textChanges(view.findViewById(R.id.edit_order_name) as AppCompatEditText)
        val orderPostalCode = RxTextView.textChanges(view.findViewById(R.id.edit_order_postal_code) as AppCompatEditText)
        val orderAddressFirst = RxTextView.textChanges(view.findViewById(R.id.edit_order_address_first) as AppCompatEditText)
        val orderAddressSecond = RxTextView.textChanges(view.findViewById(R.id.edit_order_address_second) as AppCompatEditText)

        // Buttonを取得
        val submitButton = view.findViewById(R.id.button_rx_submit) as AppCompatButton
        submitButton.isEnabled = false

        // ObservableをcombineLatestに食わして各項目のどれかに変更があった場合はStreamが流れるようにする
        // 引数の最後のAction1で流れてきた値をチェックしてすべて入力済ならtrueを流す
        Observable.combineLatest(orderName,
                orderPostalCode,
                orderAddressFirst,
                orderAddressSecond,
                { v1, v2, v3, v4 -> (0 < v1.length && 0 < v2.length && 0 < v3.length && 0 < v4.length)
        }).subscribe({isValid ->
            // 従いボタンの有効/無効を切り替える
            submitButton.isEnabled = isValid
        })

        return view
    }
}
23
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
24