1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package it.niedermann.owncloud.notes.model;
import android.graphics.Rect;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Px;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
public class GridItemDecoration extends SectionItemDecoration {
@NonNull
private final ItemAdapter adapter;
private final int spanCount;
private final int gutter;
public GridItemDecoration(@NonNull ItemAdapter adapter, int spanCount, @Px int sectionLeft, @Px int sectionTop, @Px int sectionRight, @Px int sectionBottom, @Px int gutter) {
super(adapter, sectionLeft, sectionTop, sectionRight, sectionBottom);
this.spanCount = spanCount;
this.adapter = adapter;
this.gutter = gutter;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
final int position = parent.getChildAdapterPosition(view);
final StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
if (adapter.getItemViewType(position) == ItemAdapter.TYPE_SECTION) {
lp.setFullSpan(true);
} else {
final int spanIndex = lp.getSpanIndex();
if (position >= 0) {
// First row gets some spacing at the top
if (position < spanCount && position < adapter.getFirstPositionOfViewType(ItemAdapter.TYPE_SECTION)) {
outRect.top = gutter;
}
// First column gets some spacing at the left and the right side
if (spanIndex == 0) {
outRect.left = gutter;
}
// All columns get some spacing at the bottom and at the right side
outRect.right = gutter;
outRect.bottom = gutter;
}
}
}
}
|