Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
双方向DataBinding
Search
takaaki7
July 15, 2016
Technology
1
1.1k
双方向DataBinding
shibuya.apk #9 2016/07/15
takaaki7
July 15, 2016
Tweet
Share
Other Decks in Technology
See All in Technology
APIファーストなプロダクトマネジメントの実践 〜SaaSus Platformでの例〜 / "Practicing API-First Product Management - An Example with SaaSus Platform
oztick139
0
110
Azureの基本的な権限管理の勉強会
yhana
0
610
Building a RAG-poweredAI chat appwith Python and VS Code
pamelafox
0
110
VSCodeの拡張機能を作っている話
ebarakazuhiro
1
590
DevOpsメトリクスとアウトカムの接続にトライ!開発プロセスを通して計測できるメトリクスの活用方法
ham0215
2
240
生産性向上チームの紹介
cybozuinsideout
PRO
1
870
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
160
Cracking the KubeCon CfP
inductor
2
250
web-application-security
matsuihidetoshi
0
170
開発パフォーマンスを最大化するための開発体制
ham0215
2
450
本当のAWS基礎
toru_kubota
0
530
ServiceNow Knowledge Learning Rise up
manarobot
0
210
Featured
See All Featured
A designer walks into a library…
pauljervisheath
200
23k
Imperfection Machines: The Place of Print at Facebook
scottboms
260
12k
Git: the NoSQL Database
bkeepers
PRO
422
63k
[RailsConf 2023] Rails as a piece of cake
palkan
23
4k
The Invisible Customer
myddelton
114
12k
Building Effective Engineering Teams - LeadDev
addyosmani
28
1.8k
GitHub's CSS Performance
jonrohan
1025
450k
How STYLIGHT went responsive
nonsquared
92
4.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
Building Your Own Lightsaber
phodgson
99
5.7k
Statistics for Hackers
jakevdp
789
220k
Bash Introduction
62gerente
604
210k
Transcript
ํDataBinding 2016/07/15 @tnakama7 shibuya.apk #9
DataBinding <layout> <data> <variable name="user" type="com.example.User"/> </data> <LinearLayout …> <TextView
android:text="@{user.name}" …/> </LinearLayout> </layout> public class User { public final ObservableField<String> name = new ObservableField<String>(); }
MVVM • Model-View-ViewModel • ViewModel: ViewΛඳը͢ΔͨΊͷঢ়ଶͷอ࣋ͱɺ View͔Βड͚औͬͨೖྗΛదͳܗʹมͯ͠Model ʹୡ͢ΔΛ࣋ͭɻ(Wikipedia)
ํ(2-way)DataBinding • ී௨ͷDataBindingɿ ViewModelͷΛมߋ͢Δ -> ViewʹࣗಈͰө͞ΕͯΔʂ • ํDataBindingɿ ී௨ͷόΠϯσΟϯάʹՃ͑ͯɺ Ϣʔβ͕View͔Βೖྗ
-> ࣗಈͰViewModelʹ͕ηοτ ͞ΕͯΔʂ
AndroidͷํDataBinding • https://halfthought.wordpress.com/2016/03/23/2- way-data-binding-on-android/ • Android Studio 2.1 • gradle
2.1.0
@={vm.name}
ྫ: EditText (TextView) <variable type="com.example.app.SignUpViewModel" name="viewModel"/> <EditText android:text="@={viewModel.name}" …/> <Button
android:enabled="@{viewModel.btnEnabled}" …/> public class SignUpViewModel extends BaseObservable { @Bindable private String name; public boolean getBtnEnabled() { return !TextUtils.isEmpty(name); } public String getName() { return name; } public void setName() { this.name = name; notifyPropertyChanged(BR.name); notifyPropertyChanged(BR.btnEnabled); } … }
RadioGroup <RadioGroup android:checkedButton="@={viewModel.gender}" …> <RadioButton android:id="@+id/male" …/> <RadioButton android:id="@+id/female" …/>
</RadioGroup> public class SignUpViewModel{ public final ObservableInt gender = new ObservableInt(); ... }
ࣗͰͰ͖Δ͠
ࣗͰΖ͏ͱ͢Δ public class SignUpViewModel extends BaseObservable{ @Bindable public String getName()
{ return name; } public void setName(String name) { this.name = name; notifyPropertyChanged(BR.name); } // Ͳ͔͜ͰnameEditTextʹηοτ͢Δ public SimpleTextWatcher nameWatcher = new SimpleTextWatcher() { @Override public void onTextChanged(String value) { setName(value); } }; /*……*/ }
ແݶϧʔϓൃੜͯ͠͠·͏ͷͰ ੍ޚ͢Δॲཧ͕ඞཁ public void setName(String name) { this.name = name;
if (!isEditMode) { notifyPropertyChanged(BR.name); } } public SimpleTextWatcher firstNameWatcher = new SimpleTextWatcher() { @Override public void onTextChanged(String value) { isEditMode = true; setName(value); isEditMode = false; } };
ଞͷView
• AbsListView android:selectedItemPosition • CalendarView android:date • CompoundButton android:checked •
DatePicker android:year, android:month, android:day • NumberPicker android:value • RatingBar android:rating • SeekBar android:progress • TabHost android:currentTab • TimePicker android:hour, android:minute
@InverseBindingMethods({ @InverseBindingMethod(type = ColorPicker.class, attribute = "color"), }) public class
ColorPicker extends View { public void setColor(int color) { /* ... */ } public int getColor() { /* ... */ } public interface OnColorChangeListener { void onColorChange(ColorPicker view, int color); } @BindingAdapter("colorAttrChanged") public static void setColorListener(ColorPicker view, final InverseBindingListener lister) { if (colorChange == null) { view.setOnColorChangeListener(null); } else { view.setOnColorChangeListener((view1, color) -> lister.onChange()); } } } <ColorPicker app:color="@={viewModel.myColor}" …/> ࣗ࡞View
ςετ @RunWith(JUnit4.class) public class SignUpViewModelTest { private SignUpViewModel viewModel;
@Before public void setUp() { viewModel = new SignUpViewModel(null); } @Test public void setNames_FullNameIsCorrect() { viewModel.setFirstName("ࢁా"); //@BindableʹΑΔϑΟʔϧυOK viewModel.lastName.set("ଠ"); //ObservableFieldOK assertEquals(viewModel.getFullName(), "ࢁాଠ"); } }
ݒ೦(ํʹݶΒͣ) • μΠΞϩάΛͬͨೖྗॲཧɺτʔετදࣔͷViewૢ ࡞ͷॲཧͳͲɺશͯͷUIϩδοΫʹ (ํʣDataBinding͕దͳΘ͚Ͱͳ͍ͷͰɺ֤ʑ Ͱॲཧͷॻ͖ํ͕·ͪ·ͪʹͳΓҰ؏ੑ͕ͳ͘Θ͔Γͮ Β͘ͳΔ͔ɻ • XMLʹ৭ʑॻ͖͗ͯ͢ͳΜͰಈ͍ͯΔ͔Θ͔Μͳ͍ •
IDEͷػೳඍົʹͳΔ
͜Μͳػೳ͔ͳ͍͔ͳ BDUJWJUZ@NBJOYNM &EJU5FYUɹ BOESPJEJE! JEBHF@FEJU BOESPJEUFYU!\WNBHF^ʜ
·ͱΊ • ํDataBinding: ViewModelΛมߋͨ͠ΒViewʹө͞ΕͯΔɻ Viewʹೖྗͨ͠ΒViewModelมߋ͞ΕͯΔɻ • <EditText android:text="@={vm.name}" />Ͱํό ΠϯσΟϯάͰ͖Δ
• DataBinding/MVVM͕͍͍ײ͡ʹͳΓͦ͏