RecyclerView With Multiple Layouts

前端 未结 3 1147
刺人心
刺人心 2021-01-16 04:58

I am using RecyclerView to display text in CardView but I want to add a Header TextView at the top of the RecyclerView that looks like this:

3条回答
  •  走了就别回头了
    2021-01-16 05:20

    If you want to add multiple types of views (more than 2 types) and not just a header and "normal" item, CommonsWare answer is perfect. But if the two mentioned types (header and item) are enough for you, the answer can be a lot simpler (from the practical point of view).

    There are two relatively simple libraries that you can use for that:

    1. RecyclerViewHeader - super simple to use, but uses a bit "hacky" approach, that sometimes can cause problems. Suitable for relatively simple headers.

    2. HeaderRecyclerView - Suitable for any type of header. Implements approach mentioned by CommonsWare to inflate two types of views. A bit more difficult to use than RecyclerViewHeader but nothing too hard to use on a daily basis.

    For disclosure: I am the author of the RecyclerViewHeader. I'm aware of its flaws, therefore I'm not trying to promote it as one-for-all solution, but more as an interesting alternative to complicating your adapter. HeaderRecyclerView is a nice piece of code that can simplify your work. I use it myself when my RecyclerView header gets complicated.

提交回复
热议问题