Let's control the exposure of the View by connecting the boolean type property declared in the ViewModel object to xml.
Declare properties to control exposure in ViewModel.
To not expose the initial state of the View, set the property value to false.
private val _emptyLayoutVisible = MutableLiveData(false)
val emptyLayoutVisible: LiveData<Boolean>
get() = _emptyLayoutVisible
By using the property value declared in the ViewModel, the visibility property of xml is processed as a conditional statement.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:visibility="@{viewmodel.emptyLayoutVisible ? View.VISIBLE : View.GONE}"
tools:visibility="gone">
...
</ConstraintLayout>
If the property declared in the ViewModel is changed, the visibility property of xml is also changed.
In the code below, a value of true is stored if the items list is empty or null, and a value of false is stored if the items list is not empty.
_emptyLayoutVisible.value = _items.value?.isEmpty() ?: true