MutableLiveData with BindingAdapter not updating visibility of view

泄露秘密 提交于 2019-12-23 19:13:19

问题


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.


回答1:


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().



来源:https://stackoverflow.com/questions/56011321/mutablelivedata-with-bindingadapter-not-updating-visibility-of-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!