【问题标题】:Data-bound visibility for RecyclerView's empty view has no effectRecyclerView 的空视图的数据绑定可见性无效
【发布时间】:2016-08-29 18:02:19
【问题描述】:

我正在尝试通过基于此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,这应该是不可能的。有人可以解释一下为什么这没有按我的预期工作吗?

【问题讨论】:

    标签: android android-recyclerview android-databinding


    【解决方案1】:

    所以我在阅读here 后解决了这个问题,SwipeRefreshLayout 必须有一个孩子,所以我将 RecyclerView 和 RelativeLayout 包装在 FrameLayout 中。我还使用this 解决方案通过 DataBindingUtil inflate() 方法为 Fragment 的布局充气,并在 Fragment 的 onCreateView() 方法中设置绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2018-03-23
      • 2020-09-12
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多