Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Overlap items in LinearLayoutManager - RecyclerView (like stacking cards)


Is that possible to overlap the items from RecyclerView ?
I am trying that with LinearLayoutManager.
My requirements are just the same as in LinearLayoutManager and I just need to overlap the items inside from top to bottom. (like stacking cards)

I have seen some variant by using ListView. So, I figure it would be possible in RecyclerView too.But, after some times of exploration, I feel like its gonna take quite some time to implement a custom layout manager and quite an extent of understanding on this.(I looked into Dave's post about Building Custom LayoutManager )

So, now I am thinking I might just need to use ListView variation based on my requirement instead of dealing with this much complexity of Custom Layout Manager.

But, I just need to make the items inside to overlap. I feel like there might be some other direction that I haven't aware yet. Pls let me know if there is any apart from above ListView varient and Custom Layout Manager.

I will also post my findings below here.

like image 408
Aung Pyae Avatar asked Dec 24 '14 07:12

Aung Pyae


People also ask

How do I reduce space between items in RecyclerView?

Try to use cardElevation=0dp. This should remove the extra spacing between recyclerview items.

How many layouts are used in RecyclerView?

The RecyclerView library provides three layout managers, which handle the most common layout situations: LinearLayoutManager arranges the items in a one-dimensional list.


Video Answer


2 Answers

I assume you're looking for a partial overlap (e.g. deck of cards slightly fanned out). If so, this seems fairly simple with RecyclerView and a custom ItemDecoration. Here's a trivial example of one that overlaps the items by 90px vertically:

public class OverlapDecoration extends RecyclerView.ItemDecoration {    private final static int vertOverlap = -90;    @Override   public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {      outRect.set(0, vertOverlap, 0, 0);    } } 

This example hard codes the offset, but if your list items vary in height you'll need to measure and add logic for this.

Add this decoration to the RV before setting the layoutmanager. I've tried it with a StaggeredGrid but it should work with LinearLayout and Grid LM's as well.

like image 144
MojoTosh Avatar answered Sep 19 '22 17:09

MojoTosh


You can achieve using ItemDecoration of RecyclerView

set ItemDecoration :

var customAdapter = CustomListAdapter() recyclerView.addItemDecoration(MyItemDecoration()) // here set decoration in recyclerview recyclerView.layoutManager = LinearLayoutManager(context) recyclerView.adapter = customAdapter 

create ItemDecoration class :

class MyItemDecoration : RecyclerView.ItemDecoration() {      private val overlapValue = -60      override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) {         outRect.set(0, 0, 0, overlapValue)  // args is : left,top,right,bottom     } } 
like image 31
Sanjayrajsinh Avatar answered Sep 17 '22 17:09

Sanjayrajsinh