Skip to content

Commit 9d1c8e0

Browse files
committed
Added the ability to specify a min amount of spacing between cards in the Grid Layout Manager
1 parent 68c1d75 commit 9d1c8e0

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

library/src/main/java/com/devbrackets/android/recyclerext/layoutmanager/AutoColumnGridLayoutManager.java

+31-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AutoColumnGridLayoutManager extends GridLayoutManager {
2323
private SpacerDecoration spacerDecoration;
2424

2525
private int rowSpacing = 0;
26+
private int minColumnSpacing = 0;
2627
private boolean matchSpacing = false;
2728

2829
private int requestedColumnWidth;
@@ -49,7 +50,6 @@ public AutoColumnGridLayoutManager(Context context, int gridItemWidth) {
4950
@Override
5051
public void onAttachedToWindow(RecyclerView recyclerView) {
5152
super.onAttachedToWindow(recyclerView);
52-
5353
setColumnWidth(requestedColumnWidth, recyclerView);
5454
}
5555

@@ -78,9 +78,23 @@ public void onDetachedFromWindow(RecyclerView recyclerView, RecyclerView.Recycle
7878
* @param recyclerView The {@link RecyclerView} to use for determining the number of columns
7979
*/
8080
public void setColumnWidth(int gridItemWidth, RecyclerView recyclerView) {
81+
requestedColumnWidth = gridItemWidth;
8182
setSpanCount(determineColumnCount(gridItemWidth, recyclerView));
8283
}
8384

85+
/**
86+
* Sets the minimum amount of spacing there should be between columns. This will
87+
* be used when determining the number of columns possible with the gridItemWidth specified
88+
* with {@link #AutoColumnGridLayoutManager(Context, int)} or {@link #setColumnWidth(int, RecyclerView)}
89+
*
90+
* @param minColumnSpacing The minimum amount of spacing between columns on each card (this should be half the distance between cards)
91+
* @param recyclerView The {@link RecyclerView} to use for determining the number of columns
92+
*/
93+
public void setMinColumnSpacing(int minColumnSpacing, RecyclerView recyclerView) {
94+
this.minColumnSpacing = minColumnSpacing;
95+
setSpanCount(determineColumnCount(requestedColumnWidth, recyclerView));
96+
}
97+
8498
/**
8599
* Sets the amount of spacing that should be between rows. This value
86100
* will be overridden when {@link #setMatchRowAndColumnSpacing(boolean)} is set to true
@@ -120,18 +134,29 @@ private int determineColumnCount(int gridItemWidth, RecyclerView recyclerView) {
120134
return 1;
121135
}
122136

123-
//If the RecyclerView has been sized then calculate the column width and attach the spacing decoration
137+
//Calculate the number of columns possible
124138
int padding = recyclerView.getPaddingLeft() + recyclerView.getPaddingRight();
125-
int count = (recyclerView.getWidth() - padding) / gridItemWidth;
139+
int usableWidth = recyclerView.getWidth() - padding;
140+
141+
int columnCount = usableWidth / gridItemWidth;
142+
int usedColumnWidth = columnCount * gridItemWidth;
143+
int minSpacingWidth = columnCount * minColumnSpacing;
144+
145+
while (usableWidth - usedColumnWidth - minSpacingWidth < 0) {
146+
columnCount--;
147+
usedColumnWidth = columnCount * gridItemWidth;
148+
minSpacingWidth = columnCount * minColumnSpacing;
149+
}
126150

151+
//Adds or updates the spacing decoration
127152
if (spacerDecoration != null) {
128-
spacerDecoration.update(recyclerView.getWidth(), gridItemWidth, count);
153+
spacerDecoration.update(recyclerView.getWidth(), gridItemWidth, columnCount);
129154
} else {
130-
spacerDecoration = new SpacerDecoration(recyclerView.getWidth(), gridItemWidth, count);
155+
spacerDecoration = new SpacerDecoration(recyclerView.getWidth(), gridItemWidth, columnCount);
131156
recyclerView.addItemDecoration(spacerDecoration);
132157
}
133158

134-
return count;
159+
return columnCount;
135160
}
136161

137162
/**

0 commit comments

Comments
 (0)