Search code examples
gridviewandroid-recyclerviewexpandablerecyclerview-layout

Expandable RecyclerView with Grid Layout Manager as child


I want to have a RecyclerView which is foldable like expandable ListView and the children of each paren will present a GridView layout manager.

Is it possible? Can anyone direct me on how to do that? Thanks


Solution

  • There is a full example here of how implement an Expandable GridView using this library.

    Basically you use the library to group your items into sections in order to have a header for each section but you can implement it yourself.

    Then you add a GridLayoutManager, define how many columns per row (2 in this example) and set the header to have a span size of 2 columns per row and children to have a span size of 1 column per row:

        GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
        glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(sectionAdapter.getSectionItemViewType(position)) {
                    case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                        return 2;
                    default:
                        return 1;
                }
            }
        });
        recyclerView.setLayoutManager(glm);
    

    In order to expand/collapse the sections, check the use of the expanded variable in the example.