Share
Embed
The Data Binding Library
Presentation of the Data Binding Library by Benoît Quenaudon
Demo repo: https://github.com/oldergod/DataBindingDemo
Video: https://www.youtube.com/watch?v=411jp1ceiH8
Benoît Quenaudon
November 27, 2016
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
112 slides
-
Problem with Android • Setters are complicated and verbose binding.date.setCompoundDrawablesWithIntrinsicBounds(
ContextCompat.getDrawable(this, R.drawable.ic_giants), null, null, null); -
Problem with Android • Manually keep track of UI updates
textView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged() {} @Override public void onTextChanged() {} @Override public void afterTextChanged() {} }); -
Problem with Android • Not all setters in Java have
XML equivalent textView.setTypeface(); drawerLayout.setScrimColor(); -
Data Binding Library ? • A framework to connect your
UI and model ◦ Once or persistently • One XML attribute for every Java setter • Custom XML attributes • Can override android:’s XML attributes -
Data Binding Library ? • A framework to connect your
UI and model ◦ Once or persistently • One XML attribute for every Java setter • Custom XML attributes • Can override android:’s XML attributes -
Data Binding Library ? • A framework to connect your
UI and model ◦ Once or persistently • One XML attribute for every Java setter • Custom XML attributes • Can override android:’s XML attributes -
Data Binding Library ? • A framework to connect your
UI and model ◦ Once or persistently • One XML attribute for every Java setter • Custom XML attributes • Can override android:’s XML attributes -
Data Binding Library ? • A framework to connect your
UI and model ◦ Once or persistently • One XML attribute for every Java setter • Custom XML attributes • Can override android:’s XML attributes -
match.xml <android.support.constraint.ConstraintLayout> <TextView android:id="@+id/date" /> <TextView android:id="@+id/stadium" /> <include android:id="@+id/homeTeam"
layout="@layout/team" /> <include android:id="@+id/awayTeam" layout="@layout/team" /> <CheckBox android:id="@+id/showScore" /> <TextView android:id="@+id/score" /> <android.support.design.widget.BottomNavigationView /> </android.support.constraint.ConstraintLayout> -
match.xml <android.support.constraint.ConstraintLayout> <TextView android:id="@+id/date" /> <TextView android:id="@+id/stadium" /> <include android:id="@+id/homeTeam"
layout="@layout/team" /> <include android:id="@+id/awayTeam" layout="@layout/team" /> <CheckBox android:id="@+id/showScore" /> <TextView android:id="@+id/score" /> <android.support.design.widget.BottomNavigationView /> </android.support.constraint.ConstraintLayout> -
match.xml <android.support.constraint.ConstraintLayout> <TextView android:id="@+id/date" /> <TextView android:id="@+id/stadium" /> <include android:id="@+id/homeTeam"
layout="@layout/team" /> <include android:id="@+id/awayTeam" layout="@layout/team" /> <CheckBox android:id="@+id/showScore" /> <TextView android:id="@+id/score" /> <android.support.design.widget.BottomNavigationView /> </android.support.constraint.ConstraintLayout> -
match.xml <android.support.constraint.ConstraintLayout> <TextView android:id="@+id/date" /> <TextView android:id="@+id/stadium" /> <include android:id="@+id/homeTeam"
layout="@layout/team" /> <include android:id="@+id/awayTeam" layout="@layout/team" /> <CheckBox android:id="@+id/showScore" /> <TextView android:id="@+id/score" /> <android.support.design.widget.BottomNavigationView /> </android.support.constraint.ConstraintLayout> -
match.xml <android.support.constraint.ConstraintLayout> <TextView android:id="@+id/date" /> <TextView android:id="@+id/stadium" /> <include android:id="@+id/homeTeam"
layout="@layout/team" /> <include android:id="@+id/awayTeam" layout="@layout/team" /> <CheckBox android:id="@+id/showScore" /> <TextView android:id="@+id/score" /> <android.support.design.widget.BottomNavigationView /> </android.support.constraint.ConstraintLayout> -
match.xml <android.support.constraint.ConstraintLayout> <TextView android:id="@+id/date" /> <TextView android:id="@+id/stadium" /> <include android:id="@+id/homeTeam"
layout="@layout/team" /> <include android:id="@+id/awayTeam" layout="@layout/team" /> <CheckBox android:id="@+id/showScore" /> <TextView android:id="@+id/score" /> <android.support.design.widget.BottomNavigationView /> </android.support.constraint.ConstraintLayout> -
Before: XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout> <!----> </LinearLayout> After: XML
<?xml version="1.0" encoding="utf-8"?> <layout> <LinearLayout> <!----> </LinearLayout> </layout> -
Before: XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout> <!----> </LinearLayout> After: XML
<?xml version="1.0" encoding="utf-8"?> <layout> <LinearLayout> <!----> </LinearLayout> </layout> -
Before setContentView(R.layout.activity_main); activityList = (ConstraintLayout) findViewById(R.id.activity_list); dateView = (TextView) findViewById(R.id.date);
stadiumView = (TextView) findViewById(R.id.stadium); showScoreView = (CheckBox) findViewById(R.id.showScore); scoreView = (TextView) findViewById(R.id.score); bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation); View homeTeam = findViewById(R.id.homeTeam); homeTeamIconView = (ImageView) homeTeam.findViewById(R.id.teamicon); homeTeamNameView = (TextView) homeTeam.findViewById(R.id.teamName); View awayTeam = findViewById(R.id.awayTeam); M O R E -
Before: Java Match match = matchViewModel.getMatch(); scoreView.setText(match.getScore()); After: XML <TextView
android:text="@{matchVM.match.score}" /> -
Before: Java Match match = matchViewModel.getMatch(); scoreView.setText(match.getScore()); After: XML <TextView
android:text="@{matchVM.match.score}" /> -
Before: Java Match match = matchViewModel.getMatch(); scoreView.setText(match.getScore()); After: XML <TextView
android:text="@{matchVM.match.score}" /> -
Before: Java if (match.getStadium() != null) { stadiumView.setText(match.getStadium()); } else
{ stadiumView.setText(R.string.unknown_stadium); } After: XML <TextView android:text="@{matchVM.match.stadium ?? @string/unknown_stadium}" /> -
Before: Java if (match.getStadium() != null) { stadiumView.setText(match.getStadium()); } else
{ stadiumView.setText(R.string.unknown_stadium); } After: XML <TextView android:text="@{matchVM.match.stadium ?? @string/unknown_stadium}" /> -
Before: Java if (match.getStadium() != null) { stadiumView.setText(match.getStadium()); } else
{ stadiumView.setText(R.string.unknown_stadium); } After: XML <TextView android:text="@{matchVM.match.stadium ?? @string/unknown_stadium}" /> -
Before: Java <string name="year_date">%1$d年%2$d月%3$d日</string> dateView.setText( getString( R.string.year_date, match.getYear(), match.getMonth(), match.getDay()
) ); After: XML <TextView android:text="@{@string/year_date(matchVM.match.year, matchVM.match.month, matchVM.match.day)}" /> -
Before: Java <string name="year_date">%1$d年%2$d月%3$d日</string> dateView.setText( getString( R.string.year_date, match.getYear(), match.getMonth(), match.getDay()
) ); After: XML <TextView android:text="@{@string/year_date(matchVM.match.year, matchVM.match.month, matchVM.match.day)}" /> -
Before: Java <string name="year_date">%1$d年%2$d月%3$d日</string> dateView.setText( getString( R.string.year_date, match.getYear(), match.getMonth(), match.getDay()
) ); After: XML <TextView android:text="@{@string/year_date(matchVM.match.year, matchVM.match.month, matchVM.match.day)}" /> -
Before: Java scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE); @Override public void
onCheckedChanged(CompoundButton compoundButton, boolean b) { scoreView.setVisibility(b ? VISIBLE : GONE); } After: XML <TextView android:visibility="@{matchVM.match.showScore ? View.VISIBLE : View.GONE}" /> -
Before: Java scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE); @Override public void
onCheckedChanged(CompoundButton compoundButton, boolean b) { scoreView.setVisibility(b ? VISIBLE : GONE); } After: XML <CheckBox android:id="@+id/showScore" /> <TextView android:visibility="@{matchVM.match.showScore ? View.VISIBLE : View.GONE}" /> -
Before: Java scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE); @Override public void
onCheckedChanged(CompoundButton compoundButton, boolean b) { scoreView.setVisibility(b ? VISIBLE : GONE); } After: XML <TextView android:visibility="@{showScore.checked ? View.VISIBLE : View.GONE}" /> <CheckBox android:id="@+id/showScore" /> -
Before: Java scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE); @Override public void
onCheckedChanged(CompoundButton compoundButton, boolean b) { scoreView.setVisibility(b ? VISIBLE : GONE); } After: XML <TextView android:visibility="@{showScore.checked ? View.VISIBLE : View.GONE}" /> <CheckBox android:id="@+id/showScore" /> -
Before: Java scoreView.setVisibility(match.isShowScore() ? VISIBLE : GONE); @Override public void
onCheckedChanged(CompoundButton compoundButton, boolean b) { scoreView.setVisibility(b ? VISIBLE : GONE); } After: XML <TextView android:visibility="@{showScore.checked ? View.VISIBLE : View.GONE}" /> -
Before: Java bottomNavigationView.setOnNavigationItemSelectedListener(this); @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {}
After: XML <BottomNavigationView app:onNavigationItemSelectedListener="@{handler::onNavigationClick}" /> -
Before: Java bottomNavigationView.setOnNavigationItemSelectedListener(this); @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {}
After: XML <BottomNavigationView app:onNavigationItemSelectedListener="@{handler::onNavigationClick}" /> -
Before: Java showScoreView.setOnCheckedChangeListener(this); @Override public void onCheckChanged(CompoundButton compoundButton, boolean b)
{} After: XML <CheckBox app:onCheckChangeListener="@{handler::onCheckChanged}" /> -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> After: Java ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setMatchVM(matchViewModel); binding.setHandler(this); -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> After: Java ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setMatchVM(matchViewModel); binding.setHandler(this); -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> After: Java ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setMatchVM(matchViewModel); binding.setHandler(this); -
After: XML <layout> <data> <variable name="matchVM" type="class.path.MatchViewModel" /> <variable name="handler"
type="class.path.ListActivity" /> </data> <!--old layout--> </layout> After: Java ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); binding.setMatchVM(matchViewModel); binding.setHandler(this); -
After: XML <layout> <data> <variable name="team" type="class.path.Team" /> </data> ...
</layout> team.xml <layout> … <include layout="@layout/team" app:team="@{matchVM.match.homeTeam}" /> </layout> match.xml -
After: XML <layout> <data> <variable name="team" type="class.path.Team" /> </data> ...
</layout> team.xml <layout> … <include layout="@layout/team" app:team="@{matchVM.match.homeTeam}" /> </layout> match.xml -
After: XML <layout> <data> <variable name="team" type="class.path.Team" /> </data> ...
</layout> team.xml <layout> … <include layout="@layout/team" app:team="@{matchVM.match.homeTeam}" /> </layout> match.xml -
ViewModel public class MatchViewModel { private Match match; public Match
getMatch() { return match; } public void setMatch(Match match) { this.match = match; } } -
ViewModel public class MatchViewModel extends BaseObservable { private Match match;
@Bindable public Match getMatch() { return match; } public void setMatch(Match match) { this.match = match; notifyPropertyChanged(BR.match); } } -
ViewModel public class MatchViewModel extends BaseObservable { private Match match;
@Bindable public Match getMatch() { return match; } public void setMatch(Match match) { this.match = match; notifyPropertyChanged(BR.match); } } -
ViewModel public class MatchViewModel extends BaseObservable { private Match match;
@Bindable public Match getMatch() { return match; } public void setMatch(Match match) { this.match = match; notifyPropertyChanged(BR.match); } } -
ViewModel public class MatchViewModel extends BaseObservable { private Match match;
@Bindable public Match getMatch() { return match; } public void setMatch(Match match) { this.match = match; notifyPropertyChanged(BR.match); } } -
Two way Binding: Model ↔ View ModelView changes ⇒ UI
updates ModelView updates ⇐ UI changes -
Two way Binding: Model ↔ View ModelView changes ⇒ UI
updates ModelView updates ⇐ UI changes -
Data Binding Library: How ? • Zero reflection • 100%
generated Java code • Bitwise flags to make views dirty -
Data Binding Library: How ? • Zero reflection • 100%
generated Java code • Bitwise flags to make views dirty -
Data Binding Library: How ? • Zero reflection • 100%
generated Java code • Bitwise flags to make views dirty -
Data Binding Library: How ? • Zero reflection • 100%
generated Java code • Bitwise flags to make views dirty -
Data Binding Library: Generated Code • from layout/match.xml • MatchBinding.java
• matchBinding.score ◦ MatchBinding.date ◦ MatchBinding.team ◦ MatchBinding.showScore ◦ MatchBinding.bottomNavigationView -
Attributes for every java setter class Team { private int
drawableId; public int getDrawableId() {} } <ImageView app:imageResource="@{team.drawableId}" /> class ImageView > void setImageResource (int resId) -
Attributes for every java setter class Team { private int
drawableId; public int getDrawableId() {} } <ImageView app:imageResource="@{team.drawableId}" /> class ImageView > void setImageResource (int resId) -
Custom XML Attributes <ImageView app:imageUrl="@{team.imageUrl}" /> @BindingAdapter("imageUrl") public static void
loadImage(ImageView view, String url) { Picasso.with(view.getContext()) .load(url) .into(view); } -
Custom XML Attributes <ImageView app:imageUrl="@{team.imageUrl}" /> @BindingAdapter("imageUrl") public static void
loadImage(ImageView view, String url) { Picasso.with(view.getContext()) .load(url) .into(view); } -
Java Code Evaluation android:visibility="@{showScore.checked ? View.VISIBLE : View.GONE}" android:padding="@{@dimen/activity_vertical_margin /
2}" android:drawableLeft="@{isCat ? @drawable.cat : @drawable.dog}" android:onClick="@{() -> presenter.onSaveClick(task)}" -
Null Pointer Exception さようなら if (match != null && match.getHomeTeam()
!= null) { homeTeamNameView.setText(match.getHomeTeam().getFullname()); } -
Null Pointer Exception さようなら if (match != null && match.getHomeTeam()
!= null) { homeTeamNameView.setText(match.getHomeTeam().getFullname()); } android:text="@{match.team.fullname}" -
Android Studio • Gradle integration • Syntax highlighting • Code
completion • Can view/debug generated code -
Android Studio • Gradle integration • Syntax highlighting • Code
completion • Can view/debug generated code -
Android Studio • Gradle integration • Syntax highlighting • Code
completion • Can view/debug generated code -
Android Studio • Gradle integration • Syntax highlighting • Code
completion • Can view/debug generated code -
Android Studio • Gradle integration • Syntax highlighting • Code
completion • Can view/debug generated code -
Android Studio Tips • Put namespaces in <layout> <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <LinearLayout> <!----> </LinearLayout> </layout> -
Android Studio Tips • Use tools: for preview <TextView tools:text="甲子園"
/> <ImageView tools:src="@drawable/image" /> <View tools:visibility="visible" /> -
Easy to start • Can be applied to a unique
part of your app • Don’t have to use every feature ◦ Only view binding, ◦ One way data binding, ◦ Custom XML attributes... -
Want some code? Demo App • https://github.com/oldergod/DataBindingDemo ◦ 0-no-databinding ◦
1-minimal-setup ◦ 2-viewbinding ◦ 3-oneway-data-binding ◦ 4-view-binding-expression ◦ 5-binding-adapters ◦ 6-persistent-data-binding ◦ 7-event-listeners ◦ 8-twoway-data-binding -
References • DataBinding Demo App ◦ https://github.com/oldergod/DataBindingDemo • Data Bindling
Library ◦ https://developer.android.com/topic/libraries/data-binding/index.html • talk:title=”@{databinding}” ◦ https://www.youtube.com/watch?v=zYGVsTE_scI • 057: Data Binding with GDE Lisa Wray ◦ http://fragmentedpodcast.com/episodes/057/ • George Mount’s blog posts ◦ https://medium.com/@georgemount007