1

I wanted to populate a nested ArrayList. Below is the JSON response from the server. Now I want sectionName as the headerView and sectionItems as the Child view of that header. So That, this layout will look like categorized items. I have researched the same but couldn't able to find any easy and efficient solution. I don't want to create a separate flatten list because that would cost me time. Now, How can I achieve this layout. PS: I don't want to use any external library.

"data": [
{
  "sectionName": "Fruits",
  "sectionItems": [
    {
      "id": "123",
      "name": "Apple",
      "image": "url",
      "price": "120",
      "quntity": "600gm"
    },
    {
      "id": "234",
      "name": "Orange",
      "image": "url",
      "price": "340",
      "quntity": "2kg"
    },
    {
      "id": "334",
      "name": "Banana",
      "image": "url",
      "price": "340",
      "quntity": "2kg"
    }
  ]
},
{
  "sectionName": "Paints",
  "sectionItems": [
    {
      "id": "345",
      "name": "Red",
      "image": "url",
      "price": "120",
      "quntity": "1l"
    },
    {
      "id": "346",
      "name": "White",
      "image": "url",
      "price": "120",
      "quntity": "1l"
    },
    {
      "id": "347",
      "name": "Purple",
      "image": "url",
      "price": "120",
      "quntity": "1l"
    }
  ]
}

EDIT: I have implemented a nested recycler view but couldn't able to render all the child element. there are more than 80 items on the nested list but I can see only the 18. enter image description here

Vijay
  • 376
  • 4
  • 13

1 Answers1

0

To answer your question, in my opinion the best idea here is to flatten the list. There are other possibilities but do they really fit the design of the RecyclerView? I don't think so. Some time ago I wrote a blog post to show how easy you can achieve sectioned RecyclerView:

https://brona.blog/2020/06/sectioned-recyclerview-in-three-steps/

Mariusz Brona
  • 1,549
  • 10
  • 12