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

双方向DataBinding

takaaki7
July 15, 2016

 双方向DataBinding

shibuya.apk #9 2016/07/15

takaaki7

July 15, 2016
Tweet

Other Decks in Technology

Transcript

  1. 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>(); }
  2. ྫ: 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); } … }
  3. 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(); ... }
  4. ࣗ෼Ͱ΍Ζ͏ͱ͢Δ 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); } }; /*……*/ }
  5. ແݶϧʔϓൃੜͯ͠͠·͏ͷͰ ੍ޚ͢Δॲཧ͕ඞཁ 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; } };
  6. • 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
  7. @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
  8. ςετ @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(), "ࢁాଠ࿠");
 }
 }