6

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

qwertz
  • 6,206
  • 9
  • 40
  • 62
  • "I also want a couple of cards to have the full screen width" is *NOT* "i want my recyclerview to be able to display my cards in a grid of 2 columns". Full-screen sized items are mutually exclusive with your 2 columns idea. Work out what you want and/or draw a picture to illustrate please. – Shark Dec 31 '15 at 12:33
  • 2
    @Shark sorry if I wasn't clear enough. I added an image to better illustrate it – qwertz Dec 31 '15 at 12:35
  • Use a combination of layouts? Like grid, with linearlayouts for each non-fullwidth region. – Mingsheng Jan 02 '16 at 01:28
  • You can try GridLayout>LinearLayout orientation horizontal>GridLayout /LinearLayout weight 0.5. Not the most efficient implementation probably but it should be possible. I'd give you some sample code if my laptop didn't die on me a few days back... – Mingsheng Jan 02 '16 at 01:51
  • @Mingsheng could you give me any code example on how to implement this with recyclerview and layoutmanager? – qwertz Jan 04 '16 at 14:48
  • oops sorry seems I missed the main point. never really used that before yet. I'll go tinker around a bit tomorrow and see what comes up. – Mingsheng Jan 05 '16 at 14:41
  • @Mingsheng thank you very much for your help – qwertz Jan 05 '16 at 14:41
  • Don't thank me yet I haven't done anything lol. Will reply around this time tomorrow regardless of whether I can get anything out – Mingsheng Jan 05 '16 at 14:49
  • aye didn't manage to do much. i will see what i can do. – Mingsheng Jan 06 '16 at 14:55

3 Answers3

5

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.

Sean Blahovici
  • 5,350
  • 4
  • 28
  • 38
1
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams)holder.itemView.getLayoutParams();

p.setFullSpan()

shuo Han
  • 79
  • 6
0

Have a look at RecyclerView with a StaggeredGridLayoutManager

Luke
  • 1,284
  • 1
  • 12
  • 34
lage
  • 589
  • 2
  • 5
  • 18