PADMESH KUMBHAR
PADMESH KUMBHAR

Reputation: 31

Multilevel Expandable List View with headers

I am trying to implement a multilevel Expandable List view with headers that looks this. Here, in the picture, Category A & I are just headers and not clickable. The Category that expand are Category B, G, H, J. On expansion, it would look like as shown for Category B.

I wish it to be dynamic. By saying Dynamic I mean that, when i get Json as shown below, It should automatically inflate the mentioned view as shown above.

[
{
"name": "Category A",
"secondary_class": [
  {
    "name": "Category B",
    "tertiary_class": [
      {
        "name": "Category C"
      },
      {
        "name": "Category D"
      },
      {
        "name": "Category E"
      },
      {
        "name": "Category F"
      }
    ]
  },
  {
    "name": "Category G",
    "tertiary_class": []
  },
  {
    "name": "Category H",
    "tertiary_class": []
  }
]
},
{
"name": "Category I",
"secondary_class": [
  {
    "name": "Category B",
    "tertiary_class": []
  },
  {
    "name": "Category J",
    "tertiary_class": []
  }
]
}
]

Ideas that will help me implement this are also welcomed. If not, Please refer me to necessary references as I went through all possible references, but couldn't find any solution.

Upvotes: 0

Views: 128

Answers (1)

Farrokh
Farrokh

Reputation: 1167

OK, you should to define a java class,a custom recyclerview adapter, custom layout adapter and use them, but how?

java class structure should be look like this:

public class Categories{
    String name;
    ArrayList<Categories> subCategories;
}

now create a layout file that have a textview for fixed headers, and a recyclerview for sub categories(Second categories B,G,H)
then create another layout for last level categories that has a clickable layout for header, and a recyclerview for items(C,D,E,F)
you can merge this layouts into a single layout

now create a recyclerview like this:

public categoriesAdapter(ArrayList<Categories> categoriesList, Context context ,int level, ...)

in your adapter class check the level and bind appropriate layout depends the level, and reassign appropriate layout to recyclerviews,

if(level==0)//start with 0
    inner_recyclerview.setAdapter(new categoriesAdapter(currentItem.subCategories,mContext,1)

and finally create another custom recyclerview adapter for last level and use it like this

if(level==1)
    inner_recyclerview.setAdapter(new lastCategoriesAdapter(currentItem.subCategories,mContext,1)

and assign click event to clickable layouts, and check visibility of each item in layout depends on level,
is this clear or need more description?

Upvotes: 1

Related Questions