Skip to content Skip to sidebar Skip to footer

Changing Layout Managers For Different Views In Recyclerview

I implemented an expandable recyclerview with child elements that are part of the list. I followed this code. This is how it works, The implementation of ExpandableListView using

Solution 1:

You can change the layout manager to GridLayoutManager and define the "span size" for the header, for example, if you want the grid with 2 columns, the header should have span size 2 and the children span size 1:

GridLayoutManagerglm=newGridLayoutManager(getContext(), 2);
    glm.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
        @OverridepublicintgetSpanSize(int position) {
            switch(getTypeForPosition(position)) {
                case HEADER:
                    return2;
                default:
                    return1;
            }
        }
    });
    recyclerView.setLayoutManager(glm);

There is a full example of expandable grid with headers here using this library.

Solution 2:

change the layout manager to gridlayout manager and handle the span size as mentioned below

layoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
            @OverridepublicintgetSpanSize(int position) {
                int type=mAdapter.getItemViewType(position);
                if (type == "view holder type name")
                    return2;
                elsereturn1;
            }
        });

Post a Comment for "Changing Layout Managers For Different Views In Recyclerview"