Pro Yearly is on sale from $80 to $50! »

advanced data binding

advanced data binding

advanced data bindingのセッション聞いていろいろ試した話

3c4e24fd827c789cb67a9f759f057b06?s=128

Shinnosuke Kugimiya

May 28, 2016
Tweet

Transcript

  1. Advanced Data Binding kyobashi.dex #3 @kgmyshin

  2. ໨࣍ • ૒ํ޲Binding • ϝιουϦϑΝϨϯε • ϥϜμࣜ

  3. هࣄ΋͋Γ·͢ https://tech.recruit-mp.co.jp/event/post-10069/

  4. هࣄ΋͋Γ·͕͢ • هࣄ͸Google IO 2016ͷʮAdvanced Data BindingʯͷηογϣϯΛฉ͍ͨ··จࣈى͜ ͨ͠͠΋ͷ • ͜ͷLTͰ͸ηογϣϯΛฉ͍ͨޙʹࣗ෼Ͱࢼ

    ͨ͠ײ৮΍ੜ੒͞ΕΔϑΝΠϧͳͲ͔Βݟ͑ ͨ࢓૊ΈͳͲʹॏ͖Λஔ͖͍ͨͱࢥ͍·͢
  5. ಈը΋ެ։͞Εͯ͠·ͬͨ https://www.youtube.com/watch?v=DAmMN7m3wLU

  6. ૒ํ޲ Binding

  7. ૒ํ޲Binding <EditText android:text="@={user.name}" .../> • ࠓ·ͰΰϦΰϦॻ͔ͳ͚Ε͹ͳΒͳ͔ͬͨ૒ํ޲ Binding͕ҰߦͰॻ͚ΔΑ͏ʹͳͬͨɻ

  8. ࢓૊Έ(1) // ࣗಈੜ੒͞ΕΔ private InverseBindingListener editTextandroidTextA = new InverseBindingListener() {

    @Override public void onChange() { String callbackArg_0 = android.databinding.adapters.TextViewBindingAdapter.getTextString(editText); String nameUser = null; User user = mUser; if (user != null) { user.setName(callbackArg_0); } } }; • ಺෦ͰInverseBindingListenerΛ࡞੒
  9. ࢓૊Έ(2) // ϑϨʔϜϫʔΫ಺ @BindingAdapter(value = {"android:beforeTextChanged", "android:onTextChanged", "android:afterTextChanged", "android:textAttrChanged"}, requireAll

    = false) public static void setTextWatcher(TextView view, --- , final InverseBindingListener textAttrChanged) { final TextWatcher newValue; if (before == null && after == null && on == null && textAttrChanged == null) { newValue = null; } else { newValue = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (on != null) { on.onTextChanged(s, start, before, count); } if (textAttrChanged != null) { textAttrChanged.onChange(); } } : }; } if (newValue != null) { view.addTextChangedListener(newValue); } } • Ϧεφʔ͕ηοτ͞ΕΔɻ
  10. ࢓૊Έ(3) // ϑϨʔϜϫʔΫ಺ @BindingAdapter("android:text") public static void setText(TextView view, CharSequence

    text) { final CharSequence oldText = view.getText(); if (text == oldText || (text == null && oldText.length() == 0)) { return; } if (text instanceof Spanned) { if (text.equals(oldText)) { return; // No change in the spans, so don't set anything. } } else if (!haveContentsChanged(text, oldText)) { return; // No content changes, so don't set anything. } view.setText(text); } • ແݶϧʔϓ͠ͳ͍Α͏ʹɺoldTextͱ৽͍͠஋ Λ֬ೝ͍ͯ͠Δɻ
  11. ࣗ࡞BindingAdapterͰ૒ํ޲ͯ͠ΈΔ • ·ͣ͸͍ͭ΋௨ΓBindingAdapterΛ࡞੒ɻ • ࣮͸͜Ε͸NGɻແݶϧʔϓͯ͠͠·͏ɻsetDateText ͕ݺ͹ΕͯɺsetCreatedAt͕ݺ͹ΕͯɺsetDateText ͕ݺ͹ΕͯɺsetCreatedAt͕ݺ͹Εͯɺɺɺ @BindingAdapter("dateText") public static

    void setDateText(EditText editText, long date) { editText.setText(String.valueOf(date)); }
  12. ৽͍͠஋ͱݹ͍஋Λ֬ೝ • ಉ͡஋ͳΒset͠ͳ͍Α͏ʹ͢Δ @BindingAdapter("dateText") public static void setDateText(EditText editText, long

    date) { String oldValue = view.getText().toString(); String newValue = String.valueOf(date); if (oldValue.equals(newValue)) { return; // ಉ͡஋ͳΒset͠ͳ͍Α͏ʹ͢Δɻ } editText.setText(newValue); }
  13. InverseBindingAdapterΛ࡞੒͢Δ • InverseBindingAdapterΛ࡞੒͢Δ • ͜ΕΛ΋ͱʹ InverseBindingListener ͕ࣗಈੜ੒͞ ΕΔ @InverseBindingAdapter(attribute =

    "dateText", event = "android:textAttrChanged") public static long getDateText(EditText view) { String text = view.getText().toString(); return Long.parseLong(text); }
  14. ͜ΕͰ૒ํ޲Bindingͷ׬੒ public class User extends BaseObservable { @Bindable private long

    createdAt; : public long getCreatedAt() { return createdAt; } public void setCreatedAt(long createdAt) { this.createdAt = createdAt; notifyPropertyChanged(BR.createdAt); } <EditText android:id="@+id/date_edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content" app:dateText="@={user.createdAt}" />
  15. ϝιουϦϑΝϨϯε

  16. ϝιουϦϑΝϨϯε <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/date_edit_text" android:onClick="@{presenter::onClickButton1}" android:onLongClick="@{presenter::onLongClickButton1}" /> •

    onClick΍onLongClickʹؔ਺ΛࢦఆͰ͖ΔΑ͏ ʹͳͬͨɻ
  17. ͲͷΠϕϯτʹରԠ͍ͯ͠Δ͔ android:onDrag android:onClick android:onApplyWindowInsets android:onCreateContextMenu android:onFocusChange android:onGenericMotion android:onHover android:onKey android:onLongClick

    android:onSystemUiVisibilityChange android:onTouch • ͪΐͬͱυΩϡϝϯτ͕ݟ౰ͨΒͳ͔͕ͬͨ android.databinding.adapters.ViewBindingAdapterΛݟͨײ͡ɺView ʹݶͬͯ͸ԼهͷΠϕϯτʹରԠ͍ͯ͠Δ͜ͱ͕Θ͔Δɻ
  18. • ଞʹ΋͍Ζ͍Ζ͋Γͦ͏ɻ • ͨͩશ෦Ͱ͸ͳ͍ͱݴ͍ͬͯͨɻ

  19. ϝιουϦϑΝϨϯεͰࢦఆ͢ Δϝιου @BindingMethod(type = View.class, attribute = "android:onClick", method =

    "setOnClickListener"), • ಺෦ͰԼهͷΑ͏ʹఆٛ͞Ε͍ͯΔɻ • methodͷฦΓ஋ͱҾ਺(+ view)ʹ߹ΘͤΔඞཁ ͕͋Δɻ
  20. ࢓૊Έ public static class OnLongClickListenerI implements OnLongClickListener{ private Presenter value;

    public OnLongClickListenerI setValue(com.kgmyshin.databindingsample.Presenter value) { this.value = value; return value == null ? null : this; } @Override public boolean onLongClick(View arg0) { return this.value.onLongClickButton1(arg0); } } : private OnLongClickListenerI mAndroidViewViewOnLo; : this.button1.setOnLongClickListener(mAndroidViewViewOnLo); : • ListenerΛࣗಈੜ੒ͯ͠set͍ͯ͠Δ͚ͩɻ
  21. ͪΐͬͱ·ͩόΪʔ • ؔ਺ͷͱ͜ΖͰ੺ઢͰΔ͠ɺonLongClick͸࣮ࡍ ʹ͸͋Δ͠ಈ͘ͷʹʮͦΜͳͷ஌Βͳ͍ʯͬͯݴ ΘΕΔɻ

  22. ϥϜμࣜ

  23. ϥϜμࣜ <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/date_edit_text" android:onClick="@{() -> presenter.showToast(user.name)}" />

    • onClick΍onLongClickʹϥϜμࣜΛࢦఆͰ͖Δ Α͏ʹͳͬͨɻ
  24. ϥϜμࣜͰࢦఆ͢Δϝιου @BindingMethod(type = View.class, attribute = "android:onFocusChange", method = "setOnFocusChangeListener"),

    • ϝιουϦϑΝϨϯεಉ༷ • methodͷฦΓ஋ͱҾ਺(+ view)ʹ߹ΘͤΔඞཁ ͕͋Δɻ
  25. ͜Μͳײ͡ <EditText android:id="@+id/date_edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onFocusChange="@{(v, fcs) -> presenter.showToast(v, fcs,

    user.name)}" /> View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { : } }
  26. ϥϜμࣜͰࢦఆ͢Δϝιου • ϥϜμࣜͷ৔߹͚ͩҾ਺ΛলུͰ͖Δ(Ұ෦লུͱ͍͏ͷ͸Ͱ͖ͳ͍) <EditText android:id="@+id/date_edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onFocusChange="@{(v, fcs) ->

    presenter.showToast(v, fcs, user.name)}" /> <EditText android:id="@+id/date_edit_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onFocusChange=“@{(v, fcs) -> presenter.showToast(user.name)}" />
  27. ࢓૊Έ public final void _internalCallbackOnClick(int sourceId , android.view.View callbackArg_0) {

    com.kgmyshin.databindingsample.User user = mUser; com.kgmyshin.databindingsample.Presenter presenter = mPresenter; if (mPresenter != null) { if (mUser != null) { nameUser = user.getName(); presenter.showToast(nameUser); } } } • ී௨ͷjava͕ੜ੒͞ΕΔɻ
  28. Ҏ্Ͱ͢