0

I'm trying to implement an empty view solution for a RecyclerView via data binding based on this solution. I have a RecyclerView and RelativeLayout (the empty view) within a SwipeRefreshLayout. Here is the XML for the layout:

<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>

As you can see the RelativeLayout after the RecyclerView is the empty view, with their visbilities based on the size of the dataset, which is a SeriesList (extends ObservableArrayList). In the fragment containing this layout I bind the data like so:

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

So based off of this, I'm assuming when the adapter's visible SeriesList is empty, the visibilities of the RecyclerView will be GONE and the empty view will be VISIBLE. Upon testing with an empty dataset both the RecyclerView (with no items) and the empty view have a visibility of VISIBLE, which shouldn't be possible. Could someone shed some light as to why this isn't working as I expect?

Community
  • 1
  • 1
Jake Moritz
  • 843
  • 2
  • 9
  • 21

1 Answers1

1

So I ended up fixing this after reading here that SwipeRefreshLayout must have one child, so I wrapped the RecyclerView and RelativeLayout in a FrameLayout. I also used this solution to inflate the Fragment's layout with the DataBindingUtil inflate() method and setting the binding in the Fragment's onCreateView() method.

Community
  • 1
  • 1
Jake Moritz
  • 843
  • 2
  • 9
  • 21