Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MutableLiveData with BindingAdapter not updating visibility of view

My BindingAdapter only runs once even though I update my LiveData multiple times.

public class ButtonViewBindingAdapter
{
    @BindingAdapter("hideIfZero")
    public static void setHideIfZero(View view, MutableLiveData<Integer> currentPosition)
    {
        view.setVisibility(currentPosition.getValue() == 0 ? View.GONE : View.VISIBLE);
    }
}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="viewModel"
            type="com.package.PermissionsViewModel"/>

        <variable
            name="clickHandler"
            type="com.package.PermissionsActivity.ClickHandler"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/buttonNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="@null"
            android:onClick="@{() -> clickHandler.nextSlide()}"
            android:text="@string/intro_next"
            android:textColor="@android:color/white"
            />

        <Button
            android:id="@+id/buttonBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:background="@null"
            android:onClick="@{() -> clickHandler.previousSlide()}"
            android:text="@string/intro_back"
            android:textColor="@android:color/white"
            app:hideIfZero="@{viewModel.currentSlidePosition}"/>

    </RelativeLayout>
</layout>

public class PermissionsViewModel extends ViewModel
{
    private MutableLiveData<Integer> currentSlidePosition = new MutableLiveData<>();

    public PermissionsViewModel()
    {
        currentSlidePosition.setValue(0);
    }

    public void setCurrentSlidePosition(final int position)
    {
        currentSlidePosition.setValue(position);
    }

    public MutableLiveData<Integer> getCurrentSlidePosition()
    {
        return currentSlidePosition;
    }
}

When I update the value in my viewmodel the visibility does still not change. The BindingAdapter does not run more than once. What am I missing here? I want it to hide when the position is 0 otherwise it should show.

like image 819
nilsi Avatar asked May 06 '19 19:05

nilsi


2 Answers

If you fail to call setLifecycleOwner() on the binding object, data binding still works, but quietly fails to get any updates. IIRC, you get the initial value held in the LiveData at the time of binding, but nothing after that.

I just filed a feature request to get data binding to complain more visibly if you fail to call setLifecycleOwner().

like image 64
CommonsWare Avatar answered Oct 13 '22 01:10

CommonsWare


Example:

binding.lifecycleOwner = this

this being fragment/activity etc.

like image 27
Ishan Dhingra Avatar answered Oct 13 '22 01:10

Ishan Dhingra