0

I need to have a certain layout, for example a CardView if I have only one item in my ArrayList of objects fed to the adapter.

However, if I have more than one item, I need to use another layout for the rows of the RecyclerView.

I know all about getItemViewType, but if the size of the ArrayList of objects fed to the adapter of my RecyclerView changes from 1 to 5, or from 5 to 1, I need to have the layouts change.

In this case OnCreateViewHolder will not be called again. (I want to avoid re-setting the adapter every time my dataset changes)

How do I have different layouts for a RecyclerView row based on size of my dataset?

Rohan
  • 593
  • 7
  • 22
  • If there are only 5 items, why are you bothering with `RecyclerView`? – CommonsWare May 19 '18 at 19:14
  • take a look at this https://stackoverflow.com/a/24933117/9025311 –  May 19 '18 at 19:20
  • @CommonsWare I don't want to have to rewrite this if tomorrow I have 50 items(performance related) – Rohan May 19 '18 at 19:28
  • @CommonsWare The value 5 was just an example to explain the requirement – Rohan May 19 '18 at 19:31
  • @Eminem I can't see how that applies to my requirement. Are you suggesting I use Reactive programming/RxJava? – Rohan May 19 '18 at 19:33
  • 1
    OK, then why are you using a `RecyclerView` for a one-item collection? Use the `RecyclerView` for the N-item collection. Use something else for the 1-item collection. – CommonsWare May 19 '18 at 19:38
  • @CommonsWare That makes sense, I was trying to work with a single view but your solution is probably the way to go. Thank you. – Rohan May 19 '18 at 19:42
  • If your rows were more straightforward, using a `RecyclerView` for any number of items would be reasonable. In your case, it seems simpler to do it with two views. If desired, you could animate the transition between them. – CommonsWare May 19 '18 at 19:56
  • I will implement the animation @CommonsWare. – Rohan May 20 '18 at 08:36

0 Answers0