Android RecyclerView overlap items (Card stacks)
You have to write your own LayoutManager or extends LinearLayoutManager
Recyclerview - Overlap items bottom to top
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
Overlap views inside Recyclerview
It looks like you're close. The problems you're seeing here are:
- The offset you're using in the item decorator from the example you used isn't large enough - hence the black gaps
- The order in which your linear layout manager is stacking your views is from the top, which means that the row below will draw over the cell above.
To fix this, first, add a bit more offset to get rid of the black gaps.
Second, call setReverseLayout(true)
on your LinearLayoutManager
(can also be done via the constructor) - this will make it draw the bottom items first, so that the cells will draw above the cells below.
Also, you might want to play around with the elevation of the views to get that neat shadow effect, making sure that a row at index N will have a higher elevation than a row at index N+1. You could do this by calling myView.setElevation((getItemCount() - position) * SOME_DP_AMOUNT)
when binding each view in your adapter.
Android: How can I let Cardviews overlap themselves inside a Recyclerview?
You can achieve overlapping of items by using negative bottom margins for your item layout. See the documentations for details.
For example:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"/>
</android.support.v7.widget.CardView>
Overlap positions/views in recyclerview
Well, you can also add negative margin in a RecyclerView.ItemDecoration.
Example:
ItemDecorator.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) {
outRect.left = mSpace;
outRect.right = mSpace;
outRect.bottom = mSpace;
outRect.top = mSpace;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Hi");
arrayList.add("World");
arrayList.add("What");
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
//Negative margin!
ItemDecorator itemDecorator = new ItemDecorator(-30);
recyclerView.setAdapter(new CustomAdapter(arrayList));
recyclerView.addItemDecoration(itemDecorator);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
How to make recycleView Items intersected?
Edit: I erased the wrong answer, this one work perfectly.
You need to create the following class(it can be inner class if you are gone use of in one activity).
public class OverlapDecoration extends RecyclerView.ItemDecoration {
private final static int horizontalOverlap = -100;
@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(horizontalOverlap, 0, 0, 0);
}
}
Then you add an Item Decoration to your recyclerView.
mRecyclerView.addItemDecoration(new OverlapDecoration());
Items overlapping in Recycler View
Problem Solved:
public void onBindViewHolder(MyViewHolder holder, int position) {
Context ctx = holder.itemView.getContext();
View view = getViewForCard(ctx, position); // getView for the card
holder.viewGroup.removeAllViews(); // **The problem solved here**
holder.viewGroup.addView(view);
}
I removed all views from the holder.viewGroup
in the onBindViewHolder()
method within MyRecyclerAdapter
class. And there is no more overlapping :).
Recyclerview items as a stack
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));
Recycler View - Overlap items from right cuts the last one
I finally found the solution to my problem and it was clipToPadding="false"
and setting paddingRight="8dp"
on my recycler view.
<android.support.v7.widget.RecyclerView
android:id="@+id/participant_list"
android:paddingLeft="4dp"
android:paddingRight="8dp"
android:clipToPadding="false"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
Related Topics
How to Set The App Icon as The Notification Icon in The Notification Drawer
How to Stop a Service Running as Foreground
How to Add The Icon for Swipeable Tabs
Rejecting Incoming Call in Android
How to Add a Custom Item to a Navigationview with a Menu Layout
Register Receiver in a Service
Onlocationchanged Callback Is Never Called
Viewpager with Fragments - Onpause(), Onresume()
Android: How to Programmatically Set Layout_Constraintright_Torightof "Parent"
Trying to Remove Fragment from View Gives Me Nullpointerexception on Mnextanim
Coordinatorlayout Using The Viewpager's Recyclerview
Way to Protect from Lucky Patcher/Play Licensing
How to Force Audio Through The Speakers When Headphones Are Plugged In