32

I have set the negative margin for items like this:-

ItemDecoration.java

public class ItemDecorator extends RecyclerView.ItemDecoration {
    private final int mSpace;

    public ItemDecorator(int space) {
        this.mSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        if (position != 0)
            outRect.top = mSpace;
    }
}

MainActivity.java

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new ItemDecorator(-80));

This causes top item to stack lowest and next item after the top item overlaps it. I want top item to overlap its next item and so on.

Current View

enter image description here

Required View

enter image description here

Bilal Haider Makki
  • 373
  • 1
  • 3
  • 9

5 Answers5

19

Try this way and render your recycler view in reverse direction.

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);

Here is the working example GitHub Link

Tasneem
  • 793
  • 8
  • 24
7

As of 2020 there is new interface ChildDrawingOrderCallback. It defines the order of drawing elements in recycler view. Can be used like so:

class BackwardsDrawingOrderCallback : RecyclerView.ChildDrawingOrderCallback {
    override fun onGetChildDrawingOrder(childCount: Int, i: Int) = childCount - i - 1
}

And then

recyclerView.setChildDrawingOrderCallback(BackwardsDrawingOrderCallback())

So there is no need to set neither reverse order nor stack from end anymore.

Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
0

You can create overlapping Ite Decorator by using: Check this link

class OverlappingItemDecoration : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.getChildAdapterPosition(view) != RecyclerView.NO_POSITION) {
            outRect.set(0, 0, 0, - 60)
        }
    }
}
Dev Soni
  • 489
  • 4
  • 12
0

The easiest way to achieve overlap is using margin with minus values inside recycler view item like so:

image_item.xml

<com.google.android.material.imageview.ShapeableImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/authorIV"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginStart="-8dp"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_default_avatar"
    app:roundPercent="50" />
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
-3

Try overriding this method.

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}
Sridhar
  • 668
  • 1
  • 11
  • 22
  • 1
    Thanks for your answer. really appreciate it. I think you miss understood my question. i have added pictures for better understanding of my requirement. Please have a look. – Bilal Haider Makki Nov 18 '16 at 12:41