Manohar
Manohar

Reputation: 23394

Merge adapter with different layout managers

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 .

Upvotes: 1

Views: 1023

Answers (1)

Manohar
Manohar

Reputation: 23394

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 .

Upvotes: 2

Related Questions