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
Use decorator item. Add the below class below. There’s a logic that draw margin on left, right and bottom. The top margin would only be drawn for the first cell. Now you have your custom decorator, then add it to your recyclerView as below. You would do it when you setup your recyclerView layout manager etc. That’s it. All done.
When the recycler views vertically scrolls, then the selected item to be centered or on the Top First item, and then the item is automatically centered or to be the first when the item to be selected is set. Let’s understand this with an example. 1.)
I’ll show you what it’s capable of and how to implement it when rendering a list. Apart from listed data, RecyclerView has some crucial decorative elements, such as scroll bars and dividers between items.
Apart from listed data, RecyclerView has some crucial decorative elements, such as scroll bars and dividers between items. And that’s where RecyclerView.ItemDecoration can help us draw all of the elements without having to spawn any unnecessary Views while we render items and screens.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With