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

KotlinとモダンなライブラリーでAndroidアプリを作るっ

 KotlinとモダンなライブラリーでAndroidアプリを作るっ

Takuji Nishibayashi

April 02, 2016
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. build.gradle dependencies { compile 'com.google.dagger:dagger:2.1'
 kapt "com.google.dagger:dagger-compiler:2.1"
 compile 'javax.annotation:javax.annotation-api:1.2'
 }

    kapt {
 generateStubs = true
 }
 LBQUͰ,PUMJOͷΫϥεʹ ରͯ͠"15Λ࣮ߦͰ͖ΔΑ͏ʹ͢Δ ίϯύΠϧ࣌ʹ+BWBͰTUVCϑΝΠϧΛੜ੒ ͢Δ͜ͱͰɺ"15Ͱੜ੒͞ΕͨΫϥεΛ ,PUMJOͰ΋ѻ͑ΔΑ͏ʹ͢Δ
  2. Module @Module
 class AppModule(@get:Provides val context: Context) {
 @Provides fun

    notificationManager(context: Context) : NotificationManager = context.getSystemService( Context.NOTIFICATION_SERVICE) as NotificationManager
 }
  3. Module @Module
 class AppModule(@get:Provides val context: Context) {
 @Provides fun

    notificationManager(context: Context) : NotificationManager = context.getSystemService( Context.NOTIFICATION_SERVICE) as NotificationManager
 } (FUUFSʹ1SPWJEFTBOOPUBUJPOΛ͚ͭΔ
  4. Module @Module
 class AppModule(@get:Provides val context: Context) {
 @Provides fun

    notificationManager(context: Context) : NotificationManager = context.getSystemService( Context.NOTIFICATION_SERVICE) as NotificationManager
 } (FUUFSʹ1SPWJEFTBOOPUBUJPOΛ͚ͭΔ ґଘΛఏڙ͢Δϝιουʹ 1SPWJEFTBOOPUBUJPOΛ͚ͭΔ
  5. Component @Component(modules = arrayOf(AppModule::class))
 interface AppComponent {
 val context: Context


    val notificationManager: NotificationManager
 
 fun inject(mainActivity: MainActivity)
 }
  6. Component @Component(modules = arrayOf(AppModule::class))
 interface AppComponent {
 val context: Context


    val notificationManager: NotificationManager
 
 fun inject(mainActivity: MainActivity)
 } ௚઀औಘ͢ΔͨΊʹϓϩύςΟʔ ϝιουͰ΋Α͍ ΛఆٛͰ͖Δ
  7. Component @Component(modules = arrayOf(AppModule::class))
 interface AppComponent {
 val context: Context


    val notificationManager: NotificationManager
 
 fun inject(mainActivity: MainActivity)
 } ௚઀औಘ͢ΔͨΊʹϓϩύςΟʔ ϝιουͰ΋Α͍ ΛఆٛͰ͖Δ ஫ೖ͢ΔͨΊͷϝιουΛ ఆٛ͢Δ
  8. Application class class App : Application() {
 val component :

    AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 }
  9. Application class class App : Application() {
 val component :

    AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 } $PNQPOFOUΛอ࣋͢ΔͨΊͷ ϓϩύςΟʔΛ࡞Δ
  10. Application class class App : Application() {
 val component :

    AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 } $PNQPOFOUΛอ࣋͢ΔͨΊͷ ϓϩύςΟʔΛ࡞Δ MB[ZΛ࢖͏͜ͱͰ ॳճΞΫηε࣌ʹॳظԽ
  11. Application class class App : Application() {
 val component :

    AppComponent by lazy {
 DaggerAppComponent .builder().appModule(AppModule(this)).build()
 }
 override fun onCreate() {
 super.onCreate()
 }
 } $PNQPOFOUΛอ࣋͢ΔͨΊͷ ϓϩύςΟʔΛ࡞Δ MB[ZΛ࢖͏͜ͱͰ ॳճΞΫηε࣌ʹॳظԽ "15Ͱੜ੒͞ΕͨΫϥεΛ ࡞ͬͯฦ͢
  12. Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context

    : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 }
  13. Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context

    : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 } *OKFDUBOOPUBUJPOΛ͚ͭͨ QSPQFSUZ΍ϝιουʹґଘ஫ೖ͞ΕΔ
  14. Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context

    : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 } *OKFDUBOOPUBUJPOΛ͚ͭͨ QSPQFSUZ΍ϝιουʹґଘ஫ೖ͞ΕΔ MBUFJOJUΛ͚ͭΔ͜ͱͰॳظԽ͕ෆཁʹͳΓɺ ͔ͭϑΟʔϧυʹ+BWB͔Β௚઀୅ೖͰ͖ΔΑ͏ʹ
  15. Target class MainActivity : AppCompatActivity() {
 @Inject
 lateinit var context

    : Context
 
 @Inject
 lateinit var notificationManager : NotificationManager
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_main)
 app.component.inject(mainActivity = this)
 }
 } *OKFDUBOOPUBUJPOΛ͚ͭͨ QSPQFSUZ΍ϝιουʹґଘ஫ೖ͞ΕΔ MBUFJOJUΛ͚ͭΔ͜ͱͰॳظԽ͕ෆཁʹͳΓɺ ͔ͭϑΟʔϧυʹ+BWB͔Β௚઀୅ೖͰ͖ΔΑ͏ʹ $PNQPOFOUͷJOKFDUϝιουʹର৅ͷΠϯελϯε Λ౉͢ BQQ͸"QQΛऔಘ͢Δ֦ுϓϩύςΟʔ
  16. build.gradle android {
 dataBinding {
 enabled = true
 }
 }


    
 dependencies {
 kapt 'com.android.databinding:compiler:2.1.0-alpha5'
 }
 kapt {
 generateStubs = true
 }
  17. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout>
  18. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout> ϧʔτཁૉ͸MBZPVU
  19. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout> ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ
  20. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout> ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά
  21. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout> ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ +BWBಉ༷ʹJNQPSUՄೳ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά
  22. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout> ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ +BWBಉ༷ʹJNQPSUՄೳ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά Ұ෦ͷԋࢉࢠ΋࢖͑Δ
  23. ϨΠΞ΢τͷXML(Ұ෦ུ) <layout>
 <data>
 <variable name=“user" type="jp.takuji31.kotlindatabinding.User" />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener"

    />
 <import type="android.view.View" />
 </data>
 <RelativeLayout>
 <TextView android:text="Name : "/>
 <TextView android:text="@{user.name}" tools:text="namename" />
 <TextView android:text="Birthday : "
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}" />
 <TextView android:text=“@{user.birthDay}" tools:text="1970/01/01"
 android:visibility="@{user.birthDay != null ? View.VISIBLE : View.GONE}” />
 <Button android:onClick="@{randomButtonClickListener}"/>
 </RelativeLayout>
 </layout> ϧʔτཁૉ͸MBZPVU όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛఆٛ +BWBಉ༷ʹJNQPSUՄೳ ఆٛͨ͠ϓϩύςΟʔ΍ ͦͷϓϩύςΟʔΛόΠ ϯσΟϯά Ұ෦ͷԋࢉࢠ΋࢖͑Δ 0O$MJDL-JTUFOFS΋ઃఆՄ
  24. όΠϯυ͢Δ class DataClassActivity : AppCompatActivity() {
 var users = listOf(


    User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView<ActivityDataClassBinding>(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 }
  25. όΠϯυ͢Δ class DataClassActivity : AppCompatActivity() {
 var users = listOf(


    User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView<ActivityDataClassBinding>(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 } MB[Z΍OPU/VMMͷEFMFHBUFΛ࢖ͬͯ όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛ༻ҙ͢ΔͱΑ͍
  26. όΠϯυ͢Δ class DataClassActivity : AppCompatActivity() {
 var users = listOf(


    User(name = "takuji31", birthDay = "1987/03/01"),
 User(name = "takuji32", birthDay = "1987/03/02"),
 User(name = "takuji33", birthDay = "1987/03/03"),
 User(name = "takuji24884", birthDay = null)
 )
 val binding: ActivityDataClassBinding by lazy {
 DataBindingUtil.setContentView<ActivityDataClassBinding>(this, R.layout.activity_data_class)
 }
 override fun onCreate(savedInstanceState: Bundle?) {
 binding.user = users.last()
 binding.randomButtonClickListener = View.OnClickListener {
 binding.user = users[0]
 users = users.drop(1) + binding.user
 }
 }
 } MB[Z΍OPU/VMMͷEFMFHBUFΛ࢖ͬͯ όΠϯσΟϯάΫϥεͷ ϓϩύςΟʔΛ༻ҙ͢ΔͱΑ͍ ࣗಈੜ੒͞ΕΔΫϥεʹ͸ఆ໊ٛͨ͠લͱಉ ͡ϓϩύςΟʔ ਖ਼֬ʹ͸TFUUFSHFUUFS ͕ ੜ੒͞ΕΔͷͰɺηοτ͢Δͱ஋͕൓ө͞Ε Δ
  27. Field access class MainActivity : AppCompatActivity() {
 val binding :

    ActivityMainBinding by lazy {DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)} 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.recyclerView.layoutManager = LinearLayoutManager(this)
 }
 }
  28. Field access class MainActivity : AppCompatActivity() {
 val binding :

    ActivityMainBinding by lazy {DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)} 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.recyclerView.layoutManager = LinearLayoutManager(this)
 }
 } MPXFS$BNFM$BTFͰϑΟʔϧ υ͕ੜ੒͞ΕΔ
  29. มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count :

    Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }

  30. มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count :

    Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }
 มߋ؂ࢹΛ͍ͨ͠৔߹͸ #BTF0CTFSWBCMFΛܧঝ
  31. มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count :

    Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }
 มߋ؂ࢹΛ͍ͨ͠৔߹͸ #BTF0CTFSWBCMFΛܧঝ ؂ࢹ͍ͨ͠ϓϩύςΟʔͷ HFUUFSʹ#JOEBCMF BOOPUBUJPOΛ͚ͭΔ
  32. มߋ؂ࢹՄೳͳσʔλʔΫϥε class Counter : BaseObservable() {
 @get:Bindable
 var count :

    Int = 0
 set(value) {
 field = value
 notifyPropertyChanged(BR.count)
 }
 }
 มߋ؂ࢹΛ͍ͨ͠৔߹͸ #BTF0CTFSWBCMFΛܧঝ ؂ࢹ͍ͨ͠ϓϩύςΟʔͷ HFUUFSʹ#JOEBCMF BOOPUBUJPOΛ͚ͭΔ ஋͕มߋ͞Εͨ͜ͱΛ௨஌
  33. όΠϯσΟϯάΛఆٛ <layout>
 <data>
 <variable name=“counter" type=“…PropertyObserverActivity.Counter” />
 <variable name="buttonClickListener"
 type="android.view.View.OnClickListener"

    />
 </data>
 <LinearLayout>
 <TextView android:text="@{@string/count_format(counter.count)}"/>
 <Button android:onClick=“@{buttonClickListener}" />
 </LinearLayout>
 </layout>

  34. Change value class PropertyObserverActivity : AppCompatActivity() {
 val binding :

    ActivityPropertyObserverBinding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_property_observer ) } 
 override fun onCreate(savedInstanceState: Bundle?) {
 val counter = Counter()
 binding.counter = counter
 binding.buttonClickListener = View.OnClickListener {
 counter.count += 1 }
 }
 }
  35. Change value class PropertyObserverActivity : AppCompatActivity() {
 val binding :

    ActivityPropertyObserverBinding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_property_observer ) } 
 override fun onCreate(savedInstanceState: Bundle?) {
 val counter = Counter()
 binding.counter = counter
 binding.buttonClickListener = View.OnClickListener {
 counter.count += 1 }
 }
 } ஋Ληοτ͢Δ͚ͩͰ มߋ͕൓ө͞ΕΔ
  36. BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView,

    zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 }
  37. BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView,

    zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 } +WN4UBUJDBOOPUBUJPOΛ ͚ͭΔ͜ͱͰTUBUJDϝιουͱͯ͠ఆٛ͞ΕΔ
  38. BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView,

    zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 } +WN4UBUJDBOOPUBUJPOΛ ͚ͭΔ͜ͱͰTUBUJDϝιουͱͯ͠ఆٛ͞ΕΔ BOESPJEUFYUଐੑ΁஋Λ ηοτͰ͖Δ
  39. BindingAdapterΛఆٛ object Adapters {
 @JvmStatic
 @BindingAdapter("android:text")
 fun convertZonedDateTimeToString (textView: TextView,

    zonedDateTime : ZonedDateTime) {
 val timeString = zonedDateTime.format( DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm:SS"))
 textView.text = timeString
 }
 } +WN4UBUJDBOOPUBUJPOΛ ͚ͭΔ͜ͱͰTUBUJDϝιουͱͯ͠ఆٛ͞ΕΔ BOESPJEUFYUଐੑ΁஋Λ ηοτͰ͖Δ 5FYU7JFXͱ;POFE%BUF5JNF ͷ૊Έ߹Θͤͷ࣌ʹݺ͹ΕΔ
  40. ஋Ληοτ͢Δ class NowActivity : AppCompatActivity() {
 
 val binding :

    ActivityNowBinding by lazy {
 DataBindingUtil.setContentView<ActivityNowBinding>(this, R.layout.activity_now)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.now = ZonedDateTime.now()
 }
 }
  41. ஋Ληοτ͢Δ class NowActivity : AppCompatActivity() {
 
 val binding :

    ActivityNowBinding by lazy {
 DataBindingUtil.setContentView<ActivityNowBinding>(this, R.layout.activity_now)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.now = ZonedDateTime.now()
 }
 } ஋Ληοτ͢Δ
  42. BindingAdapterΛఆٛ public class Converters {
 @BindingConversion
 public static ColorDrawable convertColorStringToColor(String

    colorString) {
 return new ColorDrawable(Color.parseColor(colorString));
 }
 }

  43. BindingAdapterΛఆٛ public class Converters {
 @BindingConversion
 public static ColorDrawable convertColorStringToColor(String

    colorString) {
 return new ColorDrawable(Color.parseColor(colorString));
 }
 }
 $PMPS%SBXBCMF͕ηοτͰ͖Δଐੑʹ 4USJOHΛόΠϯυ͠Α͏ͱͨ࣌͠ʹݺ͹ΕΔ
  44. ஋Ληοτ͢Δ class ColoredActivity : AppCompatActivity() {
 
 val binding :

    ActivityColoredBinding by lazy {
 DataBindingUtil.setContentView<ActivityColoredBinding>(this, R.layout.activity_colored)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.backgroundColor = "#00FF00"
 }
 }

  45. ஋Ληοτ͢Δ class ColoredActivity : AppCompatActivity() {
 
 val binding :

    ActivityColoredBinding by lazy {
 DataBindingUtil.setContentView<ActivityColoredBinding>(this, R.layout.activity_colored)
 }
 
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 binding.backgroundColor = "#00FF00"
 }
 }
 ஋Ληοτ͢Δ
  46. Easy to make Preference model public class MyPreference( pref :

    SharedPreferences ) : SharedPreferences by pref {
 public var name : String by stringPreference("")
 }
  47. Null-Safe SharedPreferences public class MyPreference( pref : SharedPreferences ) :

    SharedPreferences by pref {
 public var firstName : String by stringPreference("")
 public var lastName : String by stringPreference("")
 public var middleName : String? by nullableStringPreference(null)
 }
  48. All Preference type support public class MyPreference(pref : SharedPreferences) :

    SharedPreferences by pref {
 public var stringValue : String by stringPreference("default value")
 public var intValue : Int by intPreference(256)
 public var longValue : Long by longPreference(256L)
 public var floatValue : Float by floatPreference(12.34f)
 public var boolValue : Boolean by booleanPreference(true)
 public var stringSetValue : Set<String> by stringSetPreference(setOf()) public var stringValue : String? by nullableStringPreference()
 public var stringSetValue : Set<String>? by nullableStringSetPreference() 
 }
  49. Easy to create custom delegate property public class ByteArrayPreference( default

    : ByteArray = byteArrayOf(), name : String? = null ) : KoreferenceDelegate<ByteArray, String>(default,name), StringPreference {
 override fun toModelValue(value: String): ByteArray {
 return Base64.decode(value, Base64.DEFAULT)
 }
 
 override fun toPreferenceValue(value: ByteArray): String {
 return Base64.encodeToString(value, Base64.DEFAULT)
 }
 }
  50. Easy to create custom delegate property public class RealmPreference( pref

    : SharedPreferences ) : SharedPreferences by pref {
 var encryptionKey : ByteArray by ByteArrayPreference()
 } val pref = getSharedPreferences("realm",Context.MODE_PRIVATE)
 val model = RealmPreference(pref)
 model.encryptionKey