Android Databinding Library

Post on 10-Aug-2015

201 views 1 download

Transcript of Android Databinding Library

Android'Data'Binding'LibraryTakuji'Nishibayashi

About&me• Takuji(Nishibayashi

• @takuji31

• Fenrir(Inc.

• Nintendo(Network(ID(available!!!

• Udemae:(BD(/(Rank:(20

About&me• Android(engineer((2010/02~)

• Loves(Kotlin

• Interest(RoboVM

Data$Binding

android'binding

Not$ac've$!

RoboBinding

Not$work$with$AppCompat$!

RxBinding

Not$tested...

One$way(only

Code%only

Xamarin'+'MvvmCross'/'Forms'etc.

Cool

Cross%pla)orm%!!!

but$..

Too#expensive#for#private#development

DIY!

Next%day

Google&I/O&2015

Android'Data'Binding'Library

!

Usage

build.gradlebuildscript { repositories { jcenter() } dependencies { classpath "com.android.tools.build:gradle:1.3.0-beta1" classpath "com.android.databinding:dataBinder:1.0-rc1" }}

build.gradle

apply plugin: 'com.android.application'apply plugin: 'com.android.databinding'

View%modelpublic class ViewModel extends BaseObservable { @Bindable public String getFirstName() {} @Bindable public String getLastName() {} @Bindable public OffsetDateTime getLastUpdated() {}

public void setFirstName(String firstName) { this.firstName = firstName; notifyPropertyChanged(BR.firstName); } public void setLastName(String lastName) { this.lastName = lastName; notifyPropertyChanged(BR.lastName); } public void setLastUpdated(OffsetDateTime lastUpdated) { this.lastUpdated = lastUpdated; notifyPropertyChanged(BR.lastUpdated); }}

Converterpublic class Converters { @BindingConversion public static String convertOffsetDateTime(OffsetDateTime dateTime) { return dateTime != null ? dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) : ""; }

private Converters() {}}

Layout'XML<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <data> <variable name="viewModel" type="jp.takuji31.databindingexample.ViewModel" /> </data> <!-- ... -->

<TextView android:text="@{viewModel.firstName + ' ' + viewModel.lastName}" /> <TextView android:text="@{viewModel.lastUpdated}" /> <!-- ... --></layout>

Ac#vity//onCreateActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);binding.setViewModel(viewModel);binding.firstNameEditText.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { viewModel.setFirstName(s.toString()); }});binding.lastNameEditText.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { viewModel.setLastName(s.toString()); }});binding.submitButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { viewModel.setLastUpdated(OffsetDateTime.now()); }});

Run

Good• Auto&data&binding&more&less&codes

• Support&most&Android&version&(2.1~)

• Binding&adapter

• Converter

Good• Expression+support

• No+more+findViewById!

• No+more+ViewHolder!

Bad• One%way%binding%only!

• Supported%future?

• Unstable%support%(Android%Studio)

• development%in%progress

• Cannot%use%Kotlin%(Kotlin%M11%used%internally)

Example

github.com/takuji31/databinding3example

Thank&you!