'Recyclerview - Overlap items bottom to top
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
Required View
Solution 1:[1]
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
Solution 2:[2]
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.
Solution 3:[3]
You an create overlapping Ite Decorator by using:
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)
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tas |
| Solution 2 | Kiryl Tkach |
| Solution 3 |


