1

I am using MergeAdapter to combine 2 adapters It works fine .

Problem is for 1 adapter I need LinearLayoutManager with single column and for second adapter I need GridLayoutManager to show items in 2 columns . Is there any way to do it ?

I found a working solution(added below) by setting it to GridLayoutManager and span size based on position . I just want to know is there a better way to do this .

Manohar
  • 22,116
  • 9
  • 108
  • 144

1 Answers1

2

Set GridLayoutManager to RecyclerView and span size based on position .

   val layoutManager = GridLayoutManager(requireContext(), 2)
   layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    if(position==0){
                        return 2  //no of colums it show occupy
                    }
                    return  1
                }
            }
   recyclerView.layoutManager = layoutManager

In this case if position in mergeadapter is 0 then the only 1 column will be show come else 2 columns will be shown .

Manohar
  • 22,116
  • 9
  • 108
  • 144
  • When you say, position in MergeAdapter, you are saying for example if MergeAdapter has 3 adapters, the position 0 is for the first adapter, 1 for the second and 2 for the thirth right ? that position is not the position of the items inside each adapter – Gastón Saillén May 30 '20 at 14:57
  • I remember it being position of the items . if 1st adapter has 10 items then position 2 will be 3rd item in first adapter – Manohar May 30 '20 at 16:57