今回はRecyclerViewのところだけにする。Dagger2はまたの機会に。 Dao RoomのDaoクラス。(一部) interfaceにFlowableを指定すると、SQLite3上で変更を検知したらonNextで流してくれる。 Room上で使用できるRxJavaのクラスは以下の通り。 Class データがない時 データがある時 変更通知
DataBindingの @InverseMethod の使い方について簡単に紹介します。 使いどころとしては2-way bindingで表示する時、値を受け取る時に変換をしたい場合に使います。 サンプル簡単なサンプルとして性別を選択するSpinnerを用意して、それとenumで定義したものを2-way bindingで受け渡し出来るようにしてみます。 enum単純なenumの定義です。 InverseMethod@InverseMethod を使って、Spinnerに表示する時と、値を受け取る時に変換するstaticメソッドを作ります。 toInt メソッドは表示時にenumの値をIntに変換して返しています。これがDataBindingで画面表示時に呼び出されます。 このメソッドに @InverseMethod をつけます。アノテーションの引数には値が変更されたときに呼び出したいメソッ
Eureka EngineeringLearn about Eureka’s engineering efforts, product developments and more.
この記事は、 Android Advent Calendar 2017 - Qiita 11日目の記事です。 RecyclerViewで複雑な画面を作りたい!そんな欲求を持っている人が多いと思います。僕もそうです。でも、1個や2個のViewTypeならまだしもたくさんのViewTypeがある場合、getItemViewTypeとかで頑張った結果つらいコードになる未来が見えます。というか見てきました。 Epoxy というアプローチ Airbnbが作ったRecyclerViewで複雑な画面を作るためのライブラリです。 github.com ModelをCustomViewやDataBinding等から自動生成し、それらを操作するControllerを書き、そのControllerをEpoxyRecyclerViewというクラスにセットする感じです。 具体的に出来ることは、READMEに書かれて
忘れそうなので書いておく。 ContextをXML内で利用したい @{context.getString(errorRes)} こういう感じでcontextを渡してあげるとよい。contextは、variableで定義する必要もないし、setContext()する必要もない。よしなにセットされる。 上の例の実際の呼び出しはこういう感じになる。 getRoot().getContext().getString(errorRes); Using resource IDs in data binding layouts その他 Android のDataBinding, @{https://t.co/3zcmNEhuC1} の式内で user が null でもぬるぽにならないのか— ひさいち (@hisaichi5518) 2017年12月28日
Data Binding ModelとLayoutファイルのプロパティを紐付け、Modelのクラスを生成して渡すとViewに表示してくれるステキな機能でAndroidではライブラリで実現されています。 WindowsフォームなどのGUIプログラミングをやったことある人は知ってる概念かもしれません。 今回はしていませんが双方向バインディングするとViewへの入力をModelに反映も出来ます。 公式のドキュメントはもちろんJava向けなのでKotlinで実装してみます。 Gradle // app gradle apply plugin: 'kotlin-kapt' ... android { ... dataBinding { enabled = true } } dependencies { ... kapt 'com.android.databinding:compiler:2.3.1'
これはReactiveStreamsのPublisherをLiveDataに変換したり、逆にLiveDataをReactiveStreamsに変換したりできます。 これを使うことで、RxJava2のFlowableをちょっといじるだけで、そのままDataBindingで使うことができます。 サンプルbuild.gradleimplementation "android.arch.lifecycle:extensions:1.0.0" implementation "android.arch.lifecycle:reactivestreams:1.0.0" implementation "io.reactivex.rxjava2:rxjava:2.1.7"ViewModelボタンを押すとカレントミリ秒を表示するだけです。(RxJava使う必要性はまったくないですが…)
初期値が要らなければシンプルですが、要る場合の実装がなんとなく面倒だったため試行錯誤しています。 初期値がない場合 EditTextに対する双方向バインディングについてはTextViewBindingAdapterが最初から存在するため、android:onTextChangedにTextViewBindingAdapter.OnTextChangedをバインドするだけでOKでしょう。 LayoutXML(抜粋) <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:onTextChanged="@{viewModel.textChanged}"/> <TextView android:layout_width
今回はDataBindingの紹介です。DataBindingはGoogle I/O 2015で発表されたレイアウトのバインディングライブラリです。ライブラリはXMLで記述されたレイアウトファイルとソースコードをブリッジする役割があります。Androidアプリ開発では、お世話になっているひとも多いかもしれませんね。 開発中に遭遇した事例が興味深かったのでブログで触れておきます。本記事は不具合をフックにDataBindingの内部構造の紹介を目的に書いています(あまり言及している資料もなかったので)。 TL;DR だれかいたら教えてほしいんですがAndroid 4.2(16~18、JB)系の端末だけDatabinding(を経由したイベントリスナー全般)が動かないで死ぬケースがあるんですがexecutePendingBindingsすると解決する件について同様の経験のある方いませんか! —
AndroidでDataBinding周りミスるとめっちゃエラー出て辛いですね。辛いのでDataBindingのエラーを抽出するスクリプトを書きました。スクリプトはかなり雑なので適宜いい感じにしてください。 extract_data_binding_error.rb #! /bin/sh exec ruby -S -x "$0" "$@" #! ruby state = 0 while str = STDIN.gets break if str.chomp == "exit" case state when 0 state = 1 if str.match(/.*Found data binding errors.*/) when 1 state = 2 if str.match(/.*e: .*/) next if state == 2 print str end end gradleの
TL;DR ObservableList#OnListChangedCallbackでObservableListに格納されているデータの変更を通知できる 変更を通知できるので「通知のあったitemのみViewを更新する」と言った処理も可能 RecyclerViewなら各コールバックメソッドに対応した変更通知が使える ListViewには変更通知として notifyDataSetChanged() しかないので同様のことをしたい場合、getViewを独自に叩くことになるのでActivity/FragmentにObservableList#OnListChangedCallbackを配置するのが早そう チャットUI等の実装にはListViewではなくてRecyclerViewを使ったほうがViewの更新回数が減りそう ObservableList DataBindingで実装されているObs
今更感がすごいが、DataBindingを使うことによってAndroidアプリケーションの実装でMVVMパターンな設計を考えやすくなったし、DroidKaigi 2017のアプリがMVVMで実装されていたりするので、自分なりに設計をまとめてみる。 全体図 他で実装されている記事を見るとDDDなりと混ぜ合わせた感じの設計がちらほら見えて、一番シンプル(かつ集合知的な知見が溜まっている)と感じたDroidKaigi/conference-app-2017のアーキテクチャを丸パクリする形になった。 github.com 何をしているかざっと書くと View Activity/Fragment/Adapter ItemといったViewは1対1で対応するViewModelを持つ 各Layout XMLには対応するViewModelをDataBindingでbindする ViewModel Viewの
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く