0

I need to display a Recyclerview that can have any amount of subheadings. Sadly I only found solutions that support a depth of one. This isn't enough for my case.

I could have something like this:

Heading 1
  Subheading 1
    Subsubheading 1
      Subsubsubheading 1
  Subheading 2
Heading 2
...

You get the idea. Futhermore, it would also be quite useful if the user can expand these headings and their content like in MS Word. How does one achieve this behaviour (if possible without external libraries)? Thank your for you support!

Anatolii
  • 14,139
  • 4
  • 35
  • 65
Josip Domazet
  • 2,246
  • 2
  • 14
  • 37
  • Maybe try something like having each header item host a recycler view, and populate that recyclerview with the same header item – user3170251 Nov 21 '19 at 20:58
  • yeah I already thought about that, but this would result in more than 10 different recyclerviews in my case and my fear is that this will perform quite poor – Josip Domazet Nov 21 '19 at 20:59
  • Another solution is to maybe have some sort of "on the fly" insertions where if, for example, `Heading 1` was clicked, your implementation would have `Heading 1`'s subheadings stored in your model object and is inserted in between `Header 1` and `Header 2` – user3170251 Nov 21 '19 at 21:21

1 Answers1

0

I ended up using a combination of the approach from user3170251 and my own. Because my headers are from different models I used an interface and all models implement that interface.

The recyclerview only gets a list of objects that implement the interface. By checking the type of the current element it knows whether it is a read-only header or a normal element with special on-click functions. Now, to have some sort of hierarchy you need to store the depth in the model itself. So the data hierarchy that the recyclerview sees is still a flat list, but by saving the depth in the model itself I can give headers with a deeper depth a smaller font size.

The answer from Anshul Aggarwal on Is there an addHeaderView equivalent for RecyclerView? really helped me out.

Josip Domazet
  • 2,246
  • 2
  • 14
  • 37