第二个盖住第一个视图:
在这里插入图片描述
item.layout

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="-10dp"
android:elevation="0dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="12dp"
app:cardElevation="0dp">

</androidx.cardview.widget.CardView>

adapter:

1
2
3
if (holder.adapterPosition == 0) {
setMargins(holder.itemView, 0, 0, 0, 0);
}
1
2
3
4
5
6
7
private fun setMargins(v: View, l: Int, t: Int, r: Int, b: Int) {
if (v.layoutParams is ViewGroup.MarginLayoutParams) {
val p = v.layoutParams as ViewGroup.MarginLayoutParams
p.setMargins(l, t, r, b)
v.requestLayout()
}
}

layout:

1
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)

第一个盖住第二个视图堆叠:
在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="-10dp"
android:elevation="0dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="12dp"
app:cardElevation="0dp">

</androidx.cardview.widget.CardView>

adapter:

1
2
3
if (holder.adapterPosition == 0) {
setMargins(holder.itemView, 0, 0, 0, 0);
}
1
2
3
4
5
6
7
private fun setMargins(v: View, l: Int, t: Int, r: Int, b: Int) {
if (v.layoutParams is ViewGroup.MarginLayoutParams) {
val p = v.layoutParams as ViewGroup.MarginLayoutParams
p.setMargins(l, t, r, b)
v.requestLayout()
}
}

layout:

1
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, true)

注意:此时数据的顺序实际是反过来的如果需要正序排列需要对设置adapter的数据集合进行倒序排列.