Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView GridLayout width full width and half width

Is there any way to archieve the following? I want my RecyclerView to be able to display my Cards in a grid of 2 columns, but I also want a couple of cards to have the full screen width, not just the half width. I tried using StaggeredGridLayoutManager to archive that, but I cannot find a method to archive that.

EDIT I want the layout to look like this: enter image description here

like image 371
qwertz Avatar asked Dec 31 '15 12:12

qwertz


2 Answers

To expand on shuo Han's answer, say you want a recyclerview with some items spanning full width and others only spanning half width. What you would do is:

yourRecyclerView.layoutManager = StaggeredGridLayoutManager(2, RecyclerView.VERTICAL)

and then inside your adapter, if you want an itemview to span the full width, you write this code in onBindViewHolder:

val layoutParams = holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
layoutParams.isFullSpan = true

Otherwise do not use anything, it will span half way.

like image 193
Sean Blahovici Avatar answered Sep 19 '22 01:09

Sean Blahovici


StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams)holder.itemView.getLayoutParams();

p.setFullSpan()

like image 33
shuo Han Avatar answered Sep 18 '22 01:09

shuo Han