Read More

Implementing Expandable Recycler View in Android Application

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

 

. . .

Comments (10)

Add Your Comment

  • 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….
  • 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
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close