RecyclerView 的空视图的数据绑定可见性无效

Data-bound visibility for RecyclerView's empty view has no effect

我正在尝试通过基于此 solution 的数据绑定为 RecyclerView 实现一个空视图解决方案。我在 SwipeRefreshLayout 中有一个 RecyclerView 和 RelativeLayout(空视图)。这是布局的 XML:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.view.View"/>
        <variable
            name="dataset"
            type="...SeriesList"/>
    </data>
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/list"
            android:name="...SeriesFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_marginTop="10dp"
            app:layoutManager="LinearLayoutManager"
            tools:context=".fragments.SeriesFragment"
            android:visibility="@{dataset.size() > 0 ? View.VISIBLE : View.GONE}"/>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="@{dataset.size() > 0 ? View.GONE : View.VISIBLE}">

            <TextView
                android:id="@+id/empty_text"
                style="@style/Base.TextAppearance.AppCompat.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/empty_image"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:width="300dp"
                android:gravity="center"
                android:textColor="#59000000" />

            <ImageView
                android:id="@+id/empty_image"
                android:layout_width="128dp"
                android:layout_height="160dp"
                android:layout_centerInParent="true"
                android:src="@drawable/empty" />
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
</layout>

正如你所见,RecyclerView 之后的 RelativeLayout 是空视图,它们的可见性基于数据集的大小,这是一个 SeriesList(扩展 ObservableArrayList)。在包含此布局的片段中,我像这样绑定数据:

    FragmentSeriesListBinding binding = FragmentSeriesListBinding.inflate(inflater);
    binding.setDataset(getmAdapter().getVisibleSeries());

因此,基于此,我假设当适配器的可见 SeriesList 为空时,RecyclerView 的可见性将消失,空视图将可见。在使用空数据集进行测试时,RecyclerView(没有项目)和空视图都具有 VISIBLE 的可见性,这不应该是可能的。有人能解释一下为什么这不像我预期的那样有效吗?

因此,在阅读 here that SwipeRefreshLayout must have one child, so I wrapped the RecyclerView and RelativeLayout in a FrameLayout. I also used 使用 DataBindingUtil inflate() 方法膨胀片段布局并在片段的 onCreateView() 方法中设置绑定的解决方案后,我最终解决了这个问题。