Expandable List with Recyclerview

Expandable ListView inside a RecyclerListView in Android

compile 'net.cachapa.expandablelayout:expandablelayout:2.9.1' use this library for expandable layout.

Using RecyclerView as the child of ExpandableListView

In the end of if block I used a HashMap to store the newly created child view, and I tried retrieve it at the third line of 'getChildView()'.

    public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final List<Assignment> assignmentList =
(List<Assignment>) getChild(listPosition, expandedListPosition);

String groupName = (String)getGroup(listPosition);
convertView = assignmentRecyclerViewList.get(new Integer(listPosition));

if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);

AssignmentAdapter assignmentAdapter;
RecyclerView recyclerViewAssignment;
RecyclerView.LayoutManager mLayoutManager;

recyclerViewAssignment = (RecyclerView) convertView
.findViewById(R.id.expandedListItem);

assignmentAdapter = new AssignmentAdapter(assignmentList);
mLayoutManager =
new LinearLayoutManager(this.context,
LinearLayoutManager.HORIZONTAL, false);

recyclerViewAssignment.setLayoutManager(mLayoutManager);
recyclerViewAssignment.setItemAnimator(new DefaultItemAnimator());
recyclerViewAssignment.setAdapter(assignmentAdapter);

assignmentRecyclerViewList.put(new Integer(listPosition), recyclerViewAssignment);
}

return convertView;
}

Then, I call the 'getChildView()' in the MainActivity inside of OnGroupExpand of ExpandableListView.

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int i) {
expandableListView.getExpandableListAdapter().getChildView(i, i, false,null,null );
}
});

Recycleview with nested expandable lists in header

You were right to search a library that does most of the work for you, but I don't like the library you picked. It does not seem very flexible and concise. I suggest to take a look at Groupie, its API is pretty clean. Also check Reddit for some discussion on libraries.

If you want to write it yourself I think you can solve it without nested Adapter's. Just create an 'expandable group' item type. Then in getItemCount() you count all items and their nested items (when expanded). Take a look at the Groupie source code.

Some additional feedback on your code:

  1. I would explicitly add the header to the list of items you give to your adapter. So instead of a List<Plants>, you rather provide a List<Item> and have a HeaderItem and PlantsItem. This way you have a clear separation between your domain models (Plants) and view models (the items) in your adapter.
  2. Your onBindViewHolder() method does way too much. Let your ViewHolder subclasses take care of that. Create an abstract ViewHolder with an abstract bindTo(Item item) method. Then in your HeaderViewHolder subclass it and do the actual work (after an instanceof check).
  3. Have a look at view binding, it can make your code more concise. (So does Kotlin.)


Related Topics



Leave a reply



Submit