Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit c8173ff

Browse files
committed
Add support for > 1 spans sized items
1 parent dc9a666 commit c8173ff

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

android-ui-toolkit/src/main/java/io/supernova/uitoolkit/recycler/GridSpacingItemDecoration.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,59 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle
5757

5858
private void getItemOffsetVertical(Rect outRect, RecyclerView parent, View child) {
5959

60-
int parentSize = parent.getWidth();
60+
int parentSize = parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight();
6161
GridLayoutManager manager = ((GridLayoutManager) parent.getLayoutManager());
6262
GridLayoutManager.LayoutParams layoutParams = ((GridLayoutManager.LayoutParams) child.getLayoutParams());
6363
int childAdapterPosition = parent.getChildAdapterPosition(child);
6464

6565
List<Pair<Integer, Integer>> calculatedSpacings = this.getCalculatedSpacings(parentSize, manager.getSpanCount());
6666
Pair<Integer, Integer> spacing = calculatedSpacings.get(layoutParams.getSpanIndex());
6767

68-
if (childAdapterPosition < manager.getSpanCount()) {
69-
// Apply horizontal spacing only to the first row
70-
outRect.set(spacing.first, 0, spacing.second, 0);
68+
// Assign left spacing
69+
outRect.left = spacing.first;
70+
71+
// Find the end span of this item
72+
if (layoutParams.getSpanSize() > 1) {
73+
// This item is larger than 1 span, find the spacing for the last span this view occupies
74+
Pair<Integer, Integer> trailingSpacing = calculatedSpacings.get(layoutParams.getSpanIndex() + layoutParams.getSpanSize() - 1);
75+
outRect.right = trailingSpacing.second;
7176
} else {
77+
// This item has span size 1, assign right spacing from the same container
78+
outRect.right = spacing.second;
79+
}
80+
81+
if (childAdapterPosition >= manager.getSpanCount()) {
7282
// Add vertical spacing in addition to horizontal spacings
73-
outRect.set(spacing.first, this.spacing, spacing.second, 0);
83+
outRect.top = this.spacing;
7484
}
7585
}
7686

7787
private void getItemOffsetHorizontal(Rect outRect, RecyclerView parent, View child) {
7888

79-
int parentSize = parent.getHeight();
89+
int parentSize = parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom();
8090
GridLayoutManager manager = ((GridLayoutManager) parent.getLayoutManager());
8191
GridLayoutManager.LayoutParams layoutParams = ((GridLayoutManager.LayoutParams) child.getLayoutParams());
8292
int childAdapterPosition = parent.getChildAdapterPosition(child);
8393

8494
List<Pair<Integer, Integer>> calculatedSpacings = this.getCalculatedSpacings(parentSize, manager.getSpanCount());
8595
Pair<Integer, Integer> spacing = calculatedSpacings.get(layoutParams.getSpanIndex());
8696

87-
if (childAdapterPosition < manager.getSpanCount()) {
88-
// Apply vertical spacing only to the first column
89-
outRect.set(0, spacing.first, 0, spacing.second);
97+
// Assign left spacing
98+
outRect.top = spacing.first;
99+
100+
// Find the end span of this item
101+
if (layoutParams.getSpanSize() > 1) {
102+
// This item is larger than 1 span, find the spacing for the last span this view occupies
103+
Pair<Integer, Integer> trailingSpacing = calculatedSpacings.get(layoutParams.getSpanIndex() + layoutParams.getSpanSize() - 1);
104+
outRect.bottom = trailingSpacing.second;
90105
} else {
106+
// This item has span size 1, assign right spacing from the same container
107+
outRect.bottom = spacing.second;
108+
}
109+
110+
if (childAdapterPosition >= manager.getSpanCount()) {
91111
// Add horizontal spacing in addition to vertical spacings
92-
outRect.set(this.spacing, spacing.first, 0, spacing.second);;
112+
outRect.left = this.spacing;
93113
}
94114
}
95115

app/src/main/java/io/supernova/toolkit/example/GridSpacingDecorationTestActivity.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ protected void onCreate(Bundle savedInstanceState) {
2323
}
2424

2525
private void initRecyclerView() {
26-
this.recyclerView.setLayoutManager(new GridLayoutManager(this, 5, GridLayoutManager.HORIZONTAL, false));
26+
GridLayoutManager manager = new GridLayoutManager(this, 5, GridLayoutManager.HORIZONTAL, false);
27+
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
28+
@Override
29+
public int getSpanSize(int position) {
30+
if (position == 8 || position == 14) {
31+
return 2;
32+
} else {
33+
return 1;
34+
}
35+
}
36+
});
37+
this.recyclerView.setLayoutManager(manager);
2738
this.recyclerView.setAdapter(new GridSpacingDecorationTestAdapter());
2839
this.recyclerView.addItemDecoration(new GridSpacingItemDecoration((int) this.getResources().getDimension(R.dimen.test_recycler_view_spacing)));
2940
}

0 commit comments

Comments
 (0)