How implement sticky footer in recyclerview

后端 未结 8 1916
时光说笑
时光说笑 2021-02-04 02:52

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 lo
8条回答
  •  眼角桃花
    2021-02-04 03:14

    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.

提交回复
热议问题