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

advanced data binding

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

advanced data binding

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

Avatar for Shinnosuke Kugimiya

Shinnosuke Kugimiya

May 28, 2016
Tweet

More Decks by Shinnosuke Kugimiya

Other Decks in Programming

Transcript

  1. ࢓૊Έ(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Λ࡞੒
  2. ࢓૊Έ(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); } } • Ϧεφʔ͕ηοτ͞ΕΔɻ
  3. ࢓૊Έ(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ͱ৽͍͠஋ Λ֬ೝ͍ͯ͠Δɻ
  4. ৽͍͠஋ͱݹ͍஋Λ֬ೝ • ಉ͡஋ͳΒ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); }
  5. InverseBindingAdapterΛ࡞੒͢Δ • InverseBindingAdapterΛ࡞੒͢Δ • ͜ΕΛ΋ͱʹ InverseBindingListener ͕ࣗಈੜ੒͞ ΕΔ @InverseBindingAdapter(attribute =

    "dateText", event = "android:textAttrChanged") public static long getDateText(EditText view) { String text = view.getText().toString(); return Long.parseLong(text); }
  6. ͜ΕͰ૒ํ޲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}" />
  7. ͲͷΠϕϯτʹରԠ͍ͯ͠Δ͔ 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 ʹݶͬͯ͸ԼهͷΠϕϯτʹରԠ͍ͯ͠Δ͜ͱ͕Θ͔Δɻ
  8. ϝιουϦϑΝϨϯεͰࢦఆ͢ Δϝιου @BindingMethod(type = View.class, attribute = "android:onClick", method =

    "setOnClickListener"), • ಺෦ͰԼهͷΑ͏ʹఆٛ͞Ε͍ͯΔɻ • methodͷฦΓ஋ͱҾ਺(+ view)ʹ߹ΘͤΔඞཁ ͕͋Δɻ
  9. ࢓૊Έ 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͍ͯ͠Δ͚ͩɻ
  10. ϥϜμࣜͰࢦఆ͢Δϝιου @BindingMethod(type = View.class, attribute = "android:onFocusChange", method = "setOnFocusChangeListener"),

    • ϝιουϦϑΝϨϯεಉ༷ • methodͷฦΓ஋ͱҾ਺(+ view)ʹ߹ΘͤΔඞཁ ͕͋Δɻ
  11. ϥϜμࣜͰࢦఆ͢Δϝιου • ϥϜμࣜͷ৔߹͚ͩҾ਺ΛলུͰ͖Δ(Ұ෦লུͱ͍͏ͷ͸Ͱ͖ͳ͍) <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)}" />
  12. ࢓૊Έ 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͕ੜ੒͞ΕΔɻ