Changing layout managers for different views in RecyclerView

后端 未结 2 770
独厮守ぢ
独厮守ぢ 2021-02-06 04:08

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

相关标签:
2条回答
  • 2021-02-06 04:34

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

    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type=mAdapter.getItemViewType(position);
                    if (type == "view holder type name")
                        return 2;
                    else
                        return 1;
                }
            });
    
    0 讨论(0)
  • 2021-02-06 04:39

    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:

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

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

    0 讨论(0)
提交回复
热议问题