Search code examples
javaandroidandroid-recyclerviewitem-decoration

Recyclerview items as a stack


Is it possible to create android Recyclerview items as a stack. More than one items need to come under the top item.Also need to see the stack items on the bottom of the previous item.

public class OverlapDecoration extends RecyclerView.ItemDecoration {

        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent,
                                   RecyclerView.State state) {
            final int itemPosition = parent.getChildAdapterPosition(view);
            if (itemPosition == 0) {
                return;
            }
            outRect.set(0, -150, 0, 0);
        }

    }

recyclerView.addItemDecoration(new OverlappDecoration());
recyclerview.setLayoutManager(new LinearLayoutManager(context));

Solution

  • Stack items from bottom using

    LinearLayoutmanager layoutManager = new LinearLayoutManager(this);
    layoutManager.setReverseLayout(true);
    

    and provide bottom margin in your decorator instead top margin

    public class OverlapDecoration extends RecyclerView.ItemDecoration {
    
            @Override
            public void getItemOffsets(Rect outRect, View view,
                                       RecyclerView parent,
                                       RecyclerView.State state) {
                final int itemPosition = parent.getChildAdapterPosition(view);
                if (itemPosition == 0) {
                    return;
                }
                outRect.set(0, 0, 0, -150);//<-- bottom
            }
    
        }
    
    recyclerView.addItemDecoration(new OverlappDecoration());
    recyclerview.setLayoutManager(new LinearLayoutManager(context));