I have RecyclerView and I need next behavior:
- if there are a lot of items (more then fits screen) - footer is last item
- if few item/no item - footer is located at screen bottom
Please advise how can I implement this behavior.
I have RecyclerView and I need next behavior:
Please advise how can I implement this behavior.
All these solutions don't work. When you minimize app and open it again, footer flies lower than the bottom of the screen and you need to scroll to see it, even if there are just 1-2 items. You can add footer view below your recycler view in xml.
pay attention - I used NestedScrollView with
and
SpaceView has weight 1 and height = 0dp
and all this stuff inside oflinear layout
andNestedScrollView has height = match_parent
, now I have footer stuck to the bottom and it is moving further when list become largerI am using a Linearlayout with weights. I created multiple values for the footer weight, it works perfectly.
You can use RecyclerView.ItemDecoration to implement this behavior.
Make sure to set match_parent for the RecyclerView height.
Please have a look at the sample application https://github.com/JohnKuper/recyclerview-sticky-footer and how it works http://sendvid.com/nbpj0806
A Huge drawback of this solution is it works correctly only after notifyDataSetChanged() throughout an application(not inside decoration). With more specific notifications it won't work properly and to support them, it requires a way more logic. Also, you can get insights from the library recyclerview-stickyheaders by eowise and improve this solution.
If you cannot forget about RecyclerView and use ListView, then go check this link out Is there an addHeaderView equivalent for RecyclerView? it has everything you need. It's about header, but it's pretty much the same, except that header is in the beginning of your list and footer is in the end.
I know, that this is an old question, but I'll add an answer for those who would search for such decision in future. It is POSSIBLE to keep last item at the bottom of the screen in case you have only few or no items and make the last item to scroll with the recyclerview when you have many items.
How to achieve. Your RecyclerView adapter should apply several view types: views, which should be shown as a list item; view, which should be shown as footer; an empty view. You may check how to put items with different views to the RecyclerView here: https://stackoverflow.com/a/29362643/6329995 Locate an empty view between your main list and the footer view. Then in onBindViewHolder for the empty view check whether your main list views and footer view take all screen. If yes - set empty view height to zero, otherwise set it to the height which appears to be not taken by items and footer. That's all. You may also update that height dynamically, when you delete/add rows. Just call notifyItemChanged for your empty space item after you update the list.
You'd also set your RecyclerView height to match_parent or exact height, NOT wrap_content!
Hope this helps.
Improvising on Dmitriy Korobeynikov and solving the problem of calling notify dataset changed