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
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
9
73k
職種の壁を溶かして開発サイクルを高速に回す~情報透明性と職種越境から考えるAIフレンドリーな職種間連携~
daitasu
0
170
会社紹介資料 / Sansan Company Profile
sansan33
PRO
6
380k
Language Update: Java
skrb
2
300
要件定義・デザインフェーズでもAIを活用して、コミュニケーションの密度を高める
kazukihayase
0
120
Agile PBL at New Grads Trainings
kawaguti
PRO
1
430
KotlinConf 2025_イベントレポート
sony
1
140
品質視点から考える組織デザイン/Organizational Design from Quality
mii3king
0
200
まずはマネコンでちゃちゃっと作ってから、それをCDKにしてみよか。
yamada_r
2
110
LLMを搭載したプロダクトの品質保証の模索と学び
qa
0
1.1k
TS-S205_昨年対比2倍以上の機能追加を実現するデータ基盤プロジェクトでのAI活用について
kaz3284
1
170
「何となくテストする」を卒業するためにプロダクトが動く仕組みを理解しよう
kawabeaver
0
410
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
330
21k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
112
20k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Mobile First: as difficult as doing things right
swwweet
224
9.9k
A Modern Web Designer's Workflow
chriscoyier
696
190k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
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͕͍͍ײ͡ʹͳΓͦ͏