Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

Required View

enter image description here

like image 909
Bilal Haider Makki Avatar asked Nov 18 '16 12:11

Bilal Haider Makki


People also ask

How to draw top margin in recyclerview?

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.

How to center items in the Recycler view?

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.)

What can I do with recyclerview?

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.

What is recyclerview itemdecoration in Android?

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.


2 Answers

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

like image 188
Tas Avatar answered Sep 20 '22 17:09

Tas


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.

like image 27
Kiryl Tkach Avatar answered Sep 21 '22 17:09

Kiryl Tkach