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.
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"; } } |
2. Preparing data for Expandable Recycler View
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); } |
3. Setting adapter to a Recycler View to make it EXPANDABLE
1 |
recyclerView.setAdapter(new SubCategoryExpandableRecyclerAdapter(mContext, parentListItems)); |
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.
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