Updated 14 December 2016
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’
SubcategoryParentListItem is our parent class that is used to store the data relevant to parent.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class SubcategoryParentListItem implements ParentListItem { private String mTitle; private List<SubcategoryChildListItem> mChildItemList; public SubcategoryParentListItem() { mTitle = "SubcategoryParentListItem"; } @Override public List<SubcategoryChildListItem> getChildItemList() { return mChildItemList; } public void setChildItemList(List<SubcategoryChildListItem> list) { mChildItemList = list; } @Override public boolean isInitiallyExpanded() { return false; } } |
SubcategoryChildListItem is our simple child class used to display the content in each child of parent
1 2 3 4 5 6 7 8 9 |
public class SubcategoryChildListItem { private final String mTitle; public SubcategoryChildListItem() { mTitle = "SubcategoryChildListItem"; } } |
Creating List of parent item to be displayed in a view.
1 2 3 4 5 |
List<SubcategoryParentListItem> subcategoryParentListItems = new ArrayList<>(); for (int i = 0; i < 5; i++) { SubcategoryParentListItem eachParentItem = new SubcategoryParentListItem(); subcategoryParentListItems.add(eachParentItem); } |
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.
1 2 3 4 5 6 7 8 9 |
List<ParentListItem> parentListItems = new ArrayList<>(); for (SubcategoryParentListItem subcategoryParentListItem : subcategoryParentListItems) { List<SubcategoryChildListItem> childItemList = new ArrayList<>(); for (int i = 0; i < 5; i++) { childItemList.add(new SubcategoryChildListItem()); } subcategoryParentListItem.setChildItemList(childItemList); parentListItems.add(subcategoryParentListItem); } |
1 |
recyclerView.setAdapter(new SubCategoryExpandableRecyclerAdapter(mContext, parentListItems)); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
class SubCategoryExpandableRecyclerAdapter extends ExpandableRecyclerAdapter<SubCategoryExpandableRecyclerAdapter.MyParentViewHolder, SubCategoryExpandableRecyclerAdapter.MyChildViewHolder> { private LayoutInflater mInflater; public SubCategoryExpandableRecyclerAdapter(Context context, List<ParentListItem> itemList) { super(itemList); mInflater = LayoutInflater.from(context); } @Override public MyParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) { View view = mInflater.inflate(R.layout.item_subcategory_fragment_elv_group, viewGroup, false); return new MyParentViewHolder(view); } @Override public MyChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) { View view = mInflater.inflate(R.layout.item_subcategory_fragment_elv_child, viewGroup, false); return new MyChildViewHolder(view); } @Override public void onBindParentViewHolder(MyParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) { SubcategoryParentListItem subcategoryParentListItem = (SubcategoryParentListItem) parentListItem; parentViewHolder.lblListHeader.setText(subcategoryParentListItem.mTitle); } @Override public void onBindChildViewHolder(MyChildViewHolder childViewHolder, int position, Object childListItem) { SubcategoryChildListItem subcategoryChildListItem = (SubcategoryChildListItem) childListItem; childViewHolder.txtListChild.setText(subcategoryChildListItem.mTitle); } public class MyParentViewHolder extends ParentViewHolder { public TextView lblListHeader; public MyParentViewHolder(View itemView) { super(itemView); lblListHeader = (TextView) itemView.findViewById(R.id.lblListHeader); } } public class MyChildViewHolder extends ChildViewHolder { public TextView txtListChild; public MyChildViewHolder(View itemView) { super(itemView); txtListChild = (TextView) itemView.findViewById(R.id.lblListItem); } } } |
That’s all folks. Stay updated !
REFERENCE:
https://github.com/bignerdranch/expandable-recycler-view
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
10 comments
Thanks in advance
childViewHolder.txtListChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your logic here
}
});
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)