Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DroidKaigi2020 - Data Bindingのイロハ

DroidKaigi2020 - Data Bindingのイロハ

DroidKaigi2020
https://droidkaigi.jp/2020/timetable/153542

English P1-85
日本語 P86-165

Android開発で最も基本となるデザインパターンはMVCパターンです。
MVCパターンではViewの操作をActivityが行うため、Activityのコードが煩雑になり保守性が下がる要因となっていました。
Data Bindingを使うとViewの振る舞いはView自身で決めるためActivityのコード量が減り保守性が上がります。
複雑になっていくUIの状態をより管理しやすくできるData Bindingを積極的に取り入れていきましょう。

本セッションではData Bindingの基礎から最新の機能まで、幅広く紹介します。
初心者歓迎です。

アジェンダ
- Data Bindingの基礎
- Data Bindingを使ってみる
- BaseObservable
- Custom ViewとData Binding
- View BindingとData Binding
- JetPack ComposeとData Binding
- Tips

Ryota Sakaguchi

February 20, 2020
Tweet

More Decks by Ryota Sakaguchi

Other Decks in Programming

Transcript

  1. Talk About • Getting Started Data Binding • What is

    Data Binding? • Basic Data Binding • BaseObservable • Custom View with Data Binding • View Binding VS Data Binding • Jetpack Compose with Data Binding • Tips
  2. Goal • Starting use Data Binding • Understanding Data Binding

    • Hope to improve your productivity • I will share this in English and Japanese • Understand all • Read like reference
  3. Version • Data Binding • Android Studio 3.5.3 • Kotlin

    1.3.61 • Android 10 emulator • View Binding • Android Studio 3.6 RC3 • Kotlin 1.3.61 • Android 10 emulator • Jetpack Componse • Android Studio 4.0 Canary6 • Kotlin 1.3.61 • Android 10 emulator
  4. • DataBindingUtil • Data Binding Library • ActivityMainBinding • Auto

    generate binding class. XML file name + Binding (Camel case) • textHello • Auto generate TextView field. android:id name (Camel case)
  5. Model‒View‒Controller Model (API, SQLite) Controller (Activity) View (XML) • Load/

    Save Data • Convert Data • Get View • Set Value to View • Handle View • Listen View Event • Post-process Event • And more… Controller controls…
  6. Data Binding Model (API, SQLite) Controller (Activity) View (XML) Data

    Binding Controller controls… • Load/ Save Data • Convert Data • Set Value to View • Handle View • Listen View Event • Post-process Event • Set Value to Data Binding Data Binding controls…
  7. What is Observable? • Subscribe Observable List<Item> Items Label 0

    Total amount 0 Buy Button disable empty Items Label 1 Total amount 110 Buy Button enable add
  8. One-way Binding activity_main.xml • Add <variable> tag • name: Valiable

    name • type: Java style type (ex: int, Date, Custom class, etc…) +
  9. Two-way Binding • Data <-> View Controller (Activity) View (XML)

    Update! Controller (Activity) View (XML) Update!
  10. New functions • DataBindingUtil.inflate(LayoutInflator, Int, ViewGroup?, Boolean) • Inflate view

    by Data Binding. Replaceable LayoutInflator.inflate() • DataBindingUtil.bind(View) • Re-binding detached view • Crash when created by other than DataBindingUtil.inflate() • executePendingBindings() !! IMPORTANT !! • Evaluate pending views. →Data Binding is update with async. →Scrolling is fast, the views will not be updated in time.
  11. What is BaseObservable? @Bindable • Auto Generated BR.{bindable name} •

    call notifyPropertyChanged() in setter I think I don’t need to use • Support Two-way Binding
  12. What is BaseObservable? nofityChange() • just call notifyChange() in setter

    • Easy and I recommended than @Bindable • Support Two-way Binding
  13. What is BaseObservable? ObservableXXX • ObservableXXX is implemented BaseOvservable •

    ObservableInt • ObservableLong • ObservableDouble • ObservableBoolean • etc… • Support Two-way Binding
  14. What is BaseObservable? • ObservableXXX has set(value) and get() •

    Why ObservableString not exist? • Use typealias! Init value • Support Two-way Binding ObservableXXX
  15. What is BaseObservable? • Getters and setters are not required

    in XML • Support Two-way Binding ObservableXXX
  16. Binding Adapter BindingAdapters.kt • Create View Extension function (appears static

    function from Java) • Add @BindingAdapter annotations layout_main.xml
  17. What is View Binding? • New in Android Studio 3.6

    • Replace findViewById • Type safe Same feature as Data Binding
  18. Setup View Binding MainActivity.kt • View Binding does not have

    DataBindingUtil • Replace inflate() or bind() ‒ +
  19. View Binding VS Data Binding TL;DR • Do you want

    to use variable binding or binding adapter? • Yes -> Data Binding • No -> View Binding • Data Binding Not required <layout>tag Required <layout> tag Easy access views Easy access views Build faster Build slower Unavailable one(two)-way binding Available one(two)-way binding Unavailable BindingAdapter Available BindingAdapter • View Binding
  20. What is Jetpack Compose? • New in Android Studio 4.0

    • Kotlin based decorative UI library • Replaceable XML layout • Jetpack Compose Version 0.1.0-dev04 What's New in Jetpack Compose (Android Dev Summit '19) https://youtu.be/dtm2h-_sNDQ
  21. Data Binding ❤ Jetpack Compose? • Jetpack compose will have

    new binding system named State (for now) • XML layout does not call composable view (for now) • Maybe I suspect Jetpack compose will not support Data Binding
  22. NullPointerException • Android Data Binding is null safety • Replaced

    default value (ex: null integer -> 0) • Safe calls (ex: viewModel.getXXX() but viewModel is null) • LogCat warning -> use SafeUnBox No Problem!
  23. Dynamic feature module • Normally, We don’t need additional approach

    for ProGuard • But, ignore from dynamic feature module • I don’t know why proguard-rules.pro
  24. URLs • Data Binding | Android Developers https://developer.android.com/topic/libraries/data-binding • View

    Binding | Android Developers https://developer.android.com/topic/libraries/view-binding • Jetpack Compose | Android Developers https://developer.android.com/jetpack/compose
  25. アジェンダ • Data Bindingのことはじめ • Data Bindingとは • Data Bindingを使ってみた

    • BaseObservable • Custom View と Data Binding • View Binding と Data Binding • Jetpack Compose と Data Binding • Tips
  26. このセッションで得られること • Data Bindingが使えるようになる • Data Bindingとは何かがわかるようになる • アプリにData Bindingを導⼊できるようになる

    • このスライドは英語版と⽇本語版を⽤意します • ⼀度に全部覚えると • 気になったところだけ⾒るのをおすすめします
  27. Version • Data Binding • Android Studio 3.5.3 • Kotlin

    1.3.61 • Android 10 emulator • View Binding • Android Studio 3.6 RC3 • Kotlin 1.3.61 • Android 10 emulator • Jetpack Componse • Android Studio 4.0 Canary6 • Kotlin 1.3.61 • Android 10 emulator
  28. • DataBindingUtil • Data Bindingライブラリにあるクラス • ActivityMainBinding • ⾃動⽣成されたクラス。クラス名は XMLファイル名

    + Binding (Camel case) • textHello • ⾃動⽣成されたTextViewの変数。android:id が名前になる (Camel case)
  29. Model‒View‒Controller Model (API, SQLite) Controller (Activity) View (XML) • データの読み込み/書き込み

    • データの変換 • Viewの取得 • Viewに値をセット • Viewの操作 • Viewのイベントを取得 • イベントの後処理 • その他… Controller がやること
  30. Data Binding Model (API, SQLite) Controller (Activity) View (XML) Data

    Binding Controller controls… • データの読み込み/書き込み • データの変換 • Viewに値をセット • Viewの操作 • Viewのイベントを取得 • イベントの後処理 • Data Bindingに値をセット Data Binding controls…
  31. Observableとは • Subscribe Observable List<Item> アイテム数 0 合計⾦額 0 購⼊ボタン

    disable empty アイテム数 1 合計⾦額 110 購⼊ボタン enable add
  32. Two-way Binding • Data <-> View Controller (Activity) View (XML)

    Update! Controller (Activity) View (XML) Update!
  33. New functions • DataBindingUtil.inflate(LayoutInflator, Int, ViewGroup?, Boolean) • LayoutInflator.inflate()のData Binding版。inflateかつBindingインスタンスを取得

    • DataBindingUtil.bind(View) • 引数のViewにData Bindingをアタッチする • DataBindingUtilでinflateしたView以外にアタッチするとクラッシュする • executePendingBindings() !! 重要 !! • 処理待ちのViewを即座に処理する →Data Binding は⾮同期で更新される →スクロールが速いと更新が間に合わないことがある
  34. View Binding VS Data Binding TL;DR • 値のバインディングかBinding Adapterを使いたい? •

    Yes -> Data Binding • No -> View Binding • Data Binding <layout>タグは不要 <layout> タグが必須 Viewの操作が簡単 Viewの操作が簡単 ビルド時間は早い ビルド時間は遅い One(two)-way bindingは使えない One(two)-way bindingが使える BindingAdapterは使えない BindingAdapterが使える • View Binding
  35. What is Jetpack Compose? • Android Studio 4.0で追加 • Kotlinベースの宣⾔UIライブラリ

    • XMLレイアウトの置き換えができる • Jetpack Compose Version 0.1.0-dev04 What's New in Jetpack Compose (Android Dev Summit '19) https://youtu.be/dtm2h-_sNDQ
  36. Data Binding ❤ Jetpack Compose? • Jetpack compose にはstateという新しいBindingの仕組みがある (今のところ)

    • XMLレイアウトの中にCompose Viewを置けない(今のところ) • おそらくJetpack composeはData Bindingをサポートしないのではないか
  37. NullPointerException • AndroidのData BindingはNull安全で動作する • 可能ならデフォルト値で置き換える(ex: null integer -> 0)

    • 関数の安全呼び出し(ex: viewModelがnullのときviewModel.getXXX()は実⾏されない) • LogCatに警告はでる -> SafeUnBoxを使うと消える ⼼配ナシ!
  38. URLs • Data Binding | Android Developers https://developer.android.com/topic/libraries/data-binding • View

    Binding | Android Developers https://developer.android.com/topic/libraries/view-binding • Jetpack Compose | Android Developers https://developer.android.com/jetpack/compose