Manish Mulimani Manish Mulimani - 5 months ago 65
Android Question

Equivalent of ListView.setEmptyView in RecyclerView

In RecyclerView, I want to set an empty view to be shown when the adapter is empty. Is there an equivalent of ListView.setEmptyView?

Answer

With the new data binding feature you can also achieve this in your layout directly:

<TextView
   android:text="No data to display."
   android:visibility="@{dataset.size() > 0 ? View.GONE : View.VISIBLE}" />

In that case you just need to add a variable and an import to the data section of your XML:

<data>
<import type="android.view.View"/>
<variable
    name="dataset"
    type="java.util.List&lt;java.lang.String&gt;"
    />
</data>