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

Kotlin & Android Data Binding Library

Kotlin & Android Data Binding Library

Takuji Nishibayashi

March 30, 2016
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. Define binding <layout>
 <data>
 <import type="android.view.View" />
 <variable name=“user" type="jp.takuji31.kotlindatabinding.DataClassActivity.User"

    />
 <variable name=“randomButtonClickListener” type="android.view.View.OnClickListener" />
 </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>
  2. Using binding class DataClassActivity : AppCompatActivity() {
 data class User(val

    name: String, val birthDay: String?)
 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
 }
 }
 }
  3. 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)
 }
 }
  4. Observable data class class Counter : BaseObservable() {
 @get:Bindable
 var

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

  5. Define binding <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>

  6. 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 }
 }
 }
  7. Define binding adapter 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
 }
 }