Simon
Simon

Reputation: 19938

Displaying ItemDecoration when there is nothing in the recyclerview

I use this timehop stickyheader library to create sticky headers for my recyclerview: https://github.com/timehop/sticky-headers-recyclerview

I have noticed that if there is no items within a recyclerview, the ItemDecorations will not appear. I don't think this is specific for the library I'm using but it applies in general for cases where one is using decorations in an empty recyclerview.

Is there a way to force the recyclerview to display the decorations even though there is nothing in the recyclerview? I know I can add an empty view to the recyclerview and 'force' the decorations to appear but I prefer not to do that.

This is for those use cases when for example, you have an activity that displays an activity feed (like in Facebook where you can see who liked your comment etc) and you want to put a header on the top of that recyclerview that will always be sticky (a header that says "Activity Feed" for example). In the case when the data is still loading on the app, there will be an empty recyclerview so the header "Activity Feed" will not show up.

Upvotes: 2

Views: 1158

Answers (1)

David Medenjak
David Medenjak

Reputation: 34532

Short answer: No. Decorations are decorations on the items. If there are no items, there can be no decoration.

So what can you do?

As you already mentionend, a possible solution would be to add an empty view type to the recyclerView. Would be a fairly easy solution, and you could even style the whole empty page. But, you'd need to modify your adapter code, add some ifs and else and it would not really be reusable.

Second possibility, if you have a header like "Activity Feed" just do it like:

<LinearLayout orientation="vertical">
  <TextView text="Header/>
  <RecyclerView height="match_parent"/>
</LinearLayout>

The recyclerview will then always have that header on top. Those sticky decorations you linked are for the purpose of displaying group headers that change, I don't see why you need that.

Third, if those are not what you want, the last possible solution would be to just extend RecyclerView and add some empty state management yourself. If empty, then show the empty view. e.g. like HERE.

Upvotes: 2

Related Questions