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.2k
双方向DataBinding
shibuya.apk #9 2016/07/15
takaaki7
July 15, 2016
Tweet
Share
Other Decks in Technology
See All in Technology
デジタルアイデンティティ人材育成推進ワーキンググループ 翻訳サブワーキンググループ 活動報告 / 20250114-OIDF-J-EduWG-TranslationSWG
oidfj
0
210
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
140
AWS re:Invent 2024 ふりかえり勉強会
yhana
0
700
.NET 9 のパフォーマンス改善
nenonaninu
0
2.2k
10年もののバグを退治した話
n_seki
0
140
20241228 - 成為最強魔法使!AI 實時生成比賽的策略 @ 2024 SD AI 年會
dpys
0
340
20241220_S3 tablesの使い方を検証してみた
handy
4
870
サーバーなしでWordPress運用、できますよ。
sogaoh
PRO
0
170
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
2
830
効率的な技術組織が作れる!書籍『チームトポロジー』要点まとめ
iwamot
2
200
大規模言語モデルとそのソフトウェア開発に向けた応用 (2024年版)
kazato
2
450
30分でわかるデータ分析者のためのディメンショナルモデリング #datatechjp / 20250120
kazaneya
PRO
16
4k
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Scaling GitHub
holman
459
140k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
2
160
Adopting Sorbet at Scale
ufuk
74
9.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Automating Front-end Workflow
addyosmani
1366
200k
A Tale of Four Properties
chriscoyier
157
23k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Visualization
eitanlees
146
15k
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͕͍͍ײ͡ʹͳΓͦ͏