Jim
Jim

Reputation: 4395

GridLayoutManager in recyclerview with space among elements and matching the full width

I want to use a gridlayout manager, the items to be in 2 columns.
I want each row to be evenly split among the items and have some small space around the items too.
I did:

val manager = GridLayoutManager(this, 4)
manager.spanSizeLookup = object: GridLayoutManager.SpanSizeLookup() {
     override fun getSpanSize(position: Int): Int = 2
}
recycler.layoutManager = manager  

but even though the items in each row are of equal size, they have a huge gap between them and have no space with items bellow.
How can I fix this?
Please note that I used a specific width/size for the inflated items in the view. I guess that is an issue but not sure what should I use for those

Upvotes: 1

Views: 1547

Answers (1)

lpizzinidev
lpizzinidev

Reputation: 13289

  1. If you need just two items for column you don't have to set the spanSizeLookup property, just set the layoutManager and add an ItemDecoration for spacing:
val manager = GridLayoutManager(this, 2)
recycler.layoutManager = manager  
recycler.addItemDecoration(DividerItemDecoration(this, manager.orientation)
  1. Make sure your recycler item layout parent view has its layout_width set to match_parent

Upvotes: 1

Related Questions