Share
Embed
Transcript
28 slides
-
هࣄ͋Γ·͕͢ • هࣄGoogle IO 2016ͷʮAdvanced Data BindingʯͷηογϣϯΛฉ͍ͨ··จࣈى͜ ͨ͠͠ͷ • ͜ͷLTͰηογϣϯΛฉ͍ͨޙʹࣗͰࢼ
ͨ͠ײ৮ੜ͞ΕΔϑΝΠϧͳͲ͔Βݟ͑ ͨΈͳͲʹॏ͖Λஔ͖͍ͨͱࢥ͍·͢ -
Έ(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) // ϑϨʔϜϫʔΫ @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) // ϑϨʔϜϫʔΫ @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ͱ৽͍͠ Λ֬ೝ͍ͯ͠Δɻ -
ࣗ࡞BindingAdapterͰํͯ͠ΈΔ • ·͍ͣͭ௨ΓBindingAdapterΛ࡞ɻ • ࣮͜ΕNGɻແݶϧʔϓͯ͠͠·͏ɻsetDateText ͕ݺΕͯɺsetCreatedAt͕ݺΕͯɺsetDateText ͕ݺΕͯɺsetCreatedAt͕ݺΕͯɺɺɺ @BindingAdapter("dateText") public static
void setDateText(EditText editText, long date) { editText.setText(String.valueOf(date)); } -
৽͍͠ͱݹ͍Λ֬ೝ • ಉ͡ͳΒ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); } -
InverseBindingAdapterΛ࡞͢Δ • InverseBindingAdapterΛ࡞͢Δ • ͜ΕΛͱʹ InverseBindingListener ͕ࣗಈੜ͞ ΕΔ @InverseBindingAdapter(attribute =
"dateText", event = "android:textAttrChanged") public static long getDateText(EditText view) { String text = view.getText().toString(); return Long.parseLong(text); } -
͜ΕͰํ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}" /> -
ϝιουϦϑΝϨϯε <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}" /> •
onClickonLongClickʹؔΛࢦఆͰ͖ΔΑ͏ ʹͳͬͨɻ -
ͲͷΠϕϯτʹରԠ͍ͯ͠Δ͔ 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 ʹݶͬͯԼهͷΠϕϯτʹରԠ͍ͯ͠Δ͜ͱ͕Θ͔Δɻ -
ϝιουϦϑΝϨϯεͰࢦఆ͢ Δϝιου @BindingMethod(type = View.class, attribute = "android:onClick", method =
"setOnClickListener"), • ෦ͰԼهͷΑ͏ʹఆٛ͞Ε͍ͯΔɻ • methodͷฦΓͱҾ(+ view)ʹ߹ΘͤΔඞཁ ͕͋Δɻ -
Έ 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͍ͯ͠Δ͚ͩɻ -
ϥϜμࣜͰࢦఆ͢Δϝιου @BindingMethod(type = View.class, attribute = "android:onFocusChange", method = "setOnFocusChangeListener"),
• ϝιουϦϑΝϨϯεಉ༷ • methodͷฦΓͱҾ(+ view)ʹ߹ΘͤΔඞཁ ͕͋Δɻ -
͜Μͳײ͡ <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) { : } } -
ϥϜμࣜͰࢦఆ͢Δϝιου • ϥϜμࣜͷ߹͚ͩҾΛলུͰ͖Δ(Ұ෦লུͱ͍͏ͷͰ͖ͳ͍) <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)}" /> -
Έ 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͕ੜ͞ΕΔɻ