はじめに
Jake Whartonが作成したRxBinding2というライブラリをご存知でしょうか。
Viewでのイベントに対して様々な条件などを付与しsubscribeすることでViewでのアクションを制御することができます。
RxBinding2をkotlinで使う場合、kotlin用に拡張されているライブラリを利用する必要があります。
RxBinding2をkotlinで使う際の情報が少なかったので苦労しました...
基本的な利用方法を記載しているので、参考になればと思います。
- Kotlin 1.3.21
- Rxbinding-kotlin 2.2.0
RxBinding2の導入
kotlin拡張用のライブラリを導入する必要があるので、-kotlin
を忘れないようにします。
// ...
dependencies {
// ...
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.5'
def rx_binding2_version = '2.2.0'
implementation "com.jakewharton.rxbinding2:rxbinding-kotlin:$rx_binding2_version"
implementation "com.jakewharton.rxbinding2:rxbinding-recyclerview-v7-kotlin:$rx_binding2_version"
implementation "com.jakewharton.rxbinding2:rxbinding-appcompat-v7-kotlin:$rx_binding2_version"
implementation "com.jakewharton.rxbinding2:rxbinding-support-v4-kotlin:$rx_binding2_version"
implementation "com.jakewharton.rxbinding2:rxbinding-design-kotlin:$rx_binding2_version"
implementation "com.jakewharton.rxbinding2:rxbinding-leanback-v17-kotlin:$rx_binding2_version"
}
利用方法
それぞれのライブラリの利用方法です。
一例を踏まえながら書き方を記載します。
rxbinding-kotlin
ViewにあるButtonに対してclickした際の挙動はこのように書くことができます。
val binding = DataBindingUtil.setContentView<ActivityHogeBinding>(this, R.layout.activity_hoge)
binding.demoButton
.clicks()
.filter { clickCount < 1 }
.subscribe {
clickCount++
Log.d("debug", "click!!")
}
rxbinding-recyclerview-v7-kotlin
ViewにあるRecyclerViewに対するアクションを制御できます。
RecyclerViewでスクロールが一番下までいったらロードする実装がこちらです。
val binding = DataBindingUtil.setContentView<ActivityHogeBinding>(this, R.layout.activity_hoge)
val gridLayoutManager = GridLayoutManager(this, 2)
// 実装例1
binding.hogeRecyclerView
.scrollEvents()
.filter { gridLayoutManager.itemCount - 1 <= gridLayoutManager.findLastVisibleItemPosition() }
.subscribe { loadMore(item) }
// 実装例2
RxRecyclerView
.scrollEvents(binding.companyRecyclerView)
.filter { gridLayoutManager.itemCount - 1 <= gridLayoutManager.findLastVisibleItemPosition() }
.subscribe { loadMore(item) }
fun loadMore(item: Item) {
if (this.itemList.last().id != item.id) {
Log.d("debug", "loadMore")
}
}
rxbinding-appcompat-v7-kotlin
AppCompat-v7 Libraryで導入できるViewに対するアクションを制御できます。
ToolBarのitemのclickだとこのようになります。
// 実装例1
toolbar.itemClicks().subscribe{ Log.d("debug", "itemClicks") }
// 実装例2
RxToolbar.itemClicks(toolbar).subscribe{ Log.d("debug", "itemClicks") }
rxbinding-support-v4-kotlin
Support-v4 Libraryで導入できるViewに対するアクションを制御できます。
SwipeRefreshLayoutのrefreshだとこのようになります。
// 実装例1
binding.hogeFragmentSwipeRefreshLayout
.refreshes()
.subscribe { Log.d("debug", "refreshes") }
// 実装例2
RxSwipeRefreshLayout
.refreshes(binding.hogeFragmentSwipeRefreshLayout)
.subscribe { Log.d("debug", "refreshes") }
rxbinding-design-kotlin
Design Support Libraryで導入できるViewに対するアクションを制御できます。
NavigationViewのitem選択時の挙動だとこのようになります。
// 実装例1
binding.demoNavigationView
.itemSelections()
.subscribe { Log.d("debug", "itemSelections") }
// 実装例2
RxNavigationView
.itemSelections(binding.demoNavigationView)
.subscribe { Log.d("debug", "itemSelections") }
rxbinding-leanback-v17-kotlin
Leanback-v17 LibraryはAndroidTV向けのライブラリです。
今回は利用していないので、rxbinding-leanback-v17-kotlinについてはパスします。
最後に
RxBinding2の基本的な使い方を書きました。
kotlinの拡張関数との組み合わせや、他のRx系のライブラリと組み合わせることで、
よりシンプルに書くこともできそうだと感じました。
リアクティブプログラミングはまだ全然慣れていないので、良い実装例があれば勉強したいので教えていただけると嬉しいです。