Help us understand the problem. What is going on with this article?

【Android】ViewModel + LiveData + DataBinding + CustomView で変更を通知させる

はじめに

DatabindingのViewModelに今まではずっとObservableFieldを使っていたのですが、LiveDataを使う+いきなりCustomViewにチャレンジしたら変更が通知されなくてハマったので対処法を書き記します。

Databinding × LiveData

ViewModelにObservableFieldだけを利用する場合との違いとして、LiveDataを利用する場合はbindingに対してsetLifecycleOwnerをする必要があります。

To use a LiveData object with your binding class, you need to specify a lifecycle owner to define the scope of the LiveData object.

https://developer.android.com/topic/libraries/data-binding/architecture
image.png

CustomView x Databinding x LiveData

しかし、Databindingを利用したCustomViewのViewModelでLiveDataを使いたい場合は、そのCustomViewを利用しているActivity / Fragmentでbinding.setLifecycleOwnerをしても変更が通知されませんでした。
どうやら、CustomView側でもsetLifecycleOwnerをする必要があるようです。

CustomView.kt
class CustomView(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {
    val binding = ViewCustomBinding.inflate(LayoutInflater.from(context), this, true).apply{
        lifecycleOwner = context as? LifecycleOwner
    }
}

こうすることで、ActivityまたはFragmentで利用しているViewであれば、LifecycleOwnerに紐付けられるようになります。

ちなみにsetLifecycleOwnerの引数はNullableなので、as?で問題ないです。
contextがLifecycleOwnerを実装していなかった場合は何も起きません。

(参考)ViewDatabinding.setLifecycleOwner
    /**
     * Sets the {@link LifecycleOwner} that should be used for observing changes of
     * LiveData in this binding. If a {@link LiveData} is in one of the binding expressions
     * and no LifecycleOwner is set, the LiveData will not be observed and updates to it
     * will not be propagated to the UI.
     *
     * @param lifecycleOwner The LifecycleOwner that should be used for observing changes of
     *                       LiveData in this binding.
     */
    @MainThread
    public void setLifecycleOwner(@Nullable LifecycleOwner lifecycleOwner) {
        if (mLifecycleOwner == lifecycleOwner) {
            return;
        }
        if (mLifecycleOwner != null) {
            mLifecycleOwner.getLifecycle().removeObserver(mOnStartListener);
        }
        mLifecycleOwner = lifecycleOwner;
        if (lifecycleOwner != null) {
            if (mOnStartListener == null) {
                mOnStartListener = new OnStartListener();
            }
            lifecycleOwner.getLifecycle().addObserver(mOnStartListener);
        }
        for (WeakListener<?> weakListener : mLocalFieldObservers) {
            if (weakListener != null) {
                weakListener.setLifecycleOwner(lifecycleOwner);
            }
        }
    }

最後に

あまりCustomViewでDatabindingって需要はないんだろうか。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away