Upgrade to Pro — share decks privately, control downloads, hide ads and more …

双方向DataBinding

 双方向DataBinding

shibuya.apk #9 2016/07/15

3bf10250943d266d0ac4bb4cde26ac60?s=128

takaaki7

July 15, 2016
Tweet

Transcript

  1. ૒ํ޲DataBinding 2016/07/15 @tnakama7 shibuya.apk #9

  2. 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>(); }
  3. MVVM • Model-View-ViewModel • ViewModel: ViewΛඳը͢ΔͨΊͷঢ়ଶͷอ࣋ͱɺ View͔Βड͚औͬͨೖྗΛద੾ͳܗʹม׵ͯ͠Model ʹ఻ୡ͢Δ໾໨Λ࣋ͭɻ(Wikipedia)

  4. ૒ํ޲(2-way)DataBinding • ී௨ͷDataBindingɿ ViewModelͷ஋Λมߋ͢Δ -> ViewʹࣗಈͰ൓ө͞ΕͯΔʂ • ૒ํ޲DataBindingɿ ී௨ͷόΠϯσΟϯάʹՃ͑ͯɺ Ϣʔβ͕View͔Βೖྗ

    -> ࣗಈͰViewModelʹ஋͕ηοτ ͞ΕͯΔʂ
  5. Androidͷ૒ํ޲DataBinding • https://halfthought.wordpress.com/2016/03/23/2- way-data-binding-on-android/ • Android Studio 2.1 • gradle

    2.1.0
  6. @={vm.name}

  7. ྫ: 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); } … }
  8. 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(); ... }
  9. ࣗ෼ͰͰ͖Δ͠

  10. ࣗ෼Ͱ΍Ζ͏ͱ͢Δ 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); } }; /*……*/ }
  11. ແݶϧʔϓൃੜͯ͠͠·͏ͷͰ ੍ޚ͢Δॲཧ͕ඞཁ 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; } };
  12. ଞͷView

  13. • 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
  14. @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
  15. ςετ @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("ଠ࿠"); //ObservableField΋OK
 assertEquals(viewModel.getFullName(), "ࢁాଠ࿠");
 }
 }
  16. ݒ೦(૒ํ޲ʹݶΒͣ) • μΠΞϩάΛ࢖ͬͨೖྗॲཧ΍ɺτʔετදࣔͷViewૢ ࡞ͷॲཧͳͲɺશͯͷUIϩδοΫʹ (૒ํ޲ʣDataBinding͕ద੾ͳΘ͚Ͱ͸ͳ͍ͷͰɺ֤ʑ Ͱॲཧͷॻ͖ํ͕·ͪ·ͪʹͳΓҰ؏ੑ͕ͳ͘Θ͔Γͮ Β͘ͳΔ͔΋ɻ • XMLʹ৭ʑॻ͖͗ͯ͢ͳΜͰಈ͍ͯΔ͔Θ͔Μͳ͍໰୊ •

    IDEͷػೳඍົʹͳΔ
  17. ͜Μͳػೳ෇͔ͳ͍͔ͳ BDUJWJUZ@NBJOYNM &EJU5FYUɹ BOESPJEJE! JEBHF@FEJU BOESPJEUFYU!\WNBHF^ʜ

  18. ·ͱΊ • ૒ํ޲DataBinding: ViewModelΛมߋͨ͠ΒViewʹ൓ө͞ΕͯΔɻ Viewʹೖྗͨ͠ΒViewModel΋มߋ͞ΕͯΔɻ • <EditText android:text="@={vm.name}" />Ͱ૒ํ޲ό ΠϯσΟϯάͰ͖Δ

    • DataBinding/MVVM͕͍͍ײ͡ʹͳΓͦ͏