Check Mobile App
Read now
Start a Project

Implementing Expandable Recycler View in Android Application

Save

Expandable Recycler view is alternative of ExpandableListView. There are many times when ExapandableListView don’t perform as per the requirement. It fails in the coordinator layout as well as have layout height issue.

Thus we are creating a simple Expandable Recycle view using bignerdranch’s expandablerecyclerview library.

Dependency used in this blog:

┬ácompile ‘com.bignerdranch.android:expandablerecyclerview:2.1.1’

1. Custom Parent and Child Classed used to represent the relationship

SubcategoryParentListItem is our parent class that is used to store the data relevant to parent.

SubcategoryChildListItem is our simple child class used to display the content in each child of parent

 

 

2. Preparing data for Expandable Recycler View

Creating List of parent item to be displayed in a view.

 

Creating a list of child item and setting this list to a particular parent.

subcategoryParentListItem.setChildItemList(childItemList); // set the list of child item for a parent item.

3. Setting adapter to a Recycler View to make it EXPANDABLE

4. Implementing Custom adapter to support expansion of views.

Unlike traditional Recycler view in which we have a single view holder and thus single onCreateViewHolder and onBindViewHolder,
We have two different sets of onCreateViewHolder and onBindViewHolder.

Screenshot_20160429-223329

 

That’s all folks. Stay updated !

 

REFERENCE:
https://github.com/bignerdranch/expandable-recycler-view

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


10 comments

  • smwaura
    How do u implement search on this Expandable Recyclerview?
  • Shudy
    How can be implmented “onclickListener” on child , to get all the information and pass, to another activity?

    Thanks in advance

    • Shubham Agarwal
      You can add OnClickListener in the SubCategoryExpandableRecyclerAdapter. This is because we are binding the views using this adapter. If you want to provide listener on Child then you do this in onBindChildViewHolder() method.

      childViewHolder.txtListChild.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      // your logic here
      }
      });

  • InstanttPS Xceleratics
    Kindly add notify datasetchanged too….
  • Shubham Agarwal
    First of all i don’t think that’s a navigation view. I think that’s a fragment rather than navigation view. But if you are planning to add expandable adapter in REcycler view check out this post https://mobikul.com/create-custom-navigation-view-recycler-view/ or https://mobikul.com/implementation-mikepenzmaterialdrawer-library-navigation-view/
    • Gaurav meghanathi
      i also created just simple recyclerview on fragment and i want to implement expnadable recyclerview like snapdeal and yahoo some navigation item to expand not each and every item to expand. and thanks to the help.
      • Shubham Agarwal
        Yes you can make a recycler view expandable. Just use a simple Recycler view with expandable recycler adapter provided by the adapter. Expandable recycler adapter allows you to set whether to expand the item or not. All depends on child items corresponding to parent item. If it is null, it will not update automatically.
  • sagar
    i m successfully add click listener to sub child. but how to expand those sub child, any idea, plz give some suggestion. thank you
    • Shubham Agarwal
      Child are automatically expanded if child item list is neither null and nor empty.
  • 4face
    Hello, where is my error?

    private List getGroups(){
    List reminderList = RealmReminders.getReminderList(mActivity, 0);
    List todayReminder = new ArrayList();
    List tomorrowReminder = new ArrayList();
    List otherDayReminder = new ArrayList();

    for (MyReminder thisReminder : reminderList){
    if (thisReminder.getRemindDay() == mThisDay) todayReminder.add(thisReminder);
    else if (thisReminder.getRemindDay() == mThisDay+1) tomorrowReminder.add(thisReminder);
    else if (thisReminder.getRemindDay() > mThisDay+1) otherDayReminder.add(thisReminder);
    }

    List list = new ArrayList();
    ReminderGroup today, tomorrow, otherDay;
    if (todayReminder.size() != 0) {
    today = new ReminderGroup(“Today”, todayReminder);
    list.add(today);
    }
    if (tomorrowReminder.size() != 0) {
    tomorrow = new ReminderGroup(“Tomorrow”, tomorrowReminder);
    list.add(tomorrow);
    }
    if (otherDayReminder.size() != 0) {
    otherDay = new ReminderGroup(“Next”, otherDayReminder);
    list.add(otherDay);
    }
    return list;
    }

    @Override
    public void onBindParentViewHolder(MyParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
    ReminderGroup reminderGroup = (ReminderGroup) parentListItem;
    parentViewHolder.day.setText(reminderGroup.getDay());
    }

    @Override
    public void onBindChildViewHolder(MyChildViewHolder childViewHolder, int position, Object childListItem) {
    MyReminder reminder = (MyReminder) childListItem;
    etc
    }

    java.lang.IllegalStateException: Null object added at com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.getItemViewType(ExpandableRecyclerAdapter.java:223)

  • css.php

    Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.

    Osama
    Talk to Sales

    Global

    Live Chat
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home