@@ -23,6 +23,7 @@ public class AutoColumnGridLayoutManager extends GridLayoutManager {
23
23
private SpacerDecoration spacerDecoration ;
24
24
25
25
private int rowSpacing = 0 ;
26
+ private int minColumnSpacing = 0 ;
26
27
private boolean matchSpacing = false ;
27
28
28
29
private int requestedColumnWidth ;
@@ -49,7 +50,6 @@ public AutoColumnGridLayoutManager(Context context, int gridItemWidth) {
49
50
@ Override
50
51
public void onAttachedToWindow (RecyclerView recyclerView ) {
51
52
super .onAttachedToWindow (recyclerView );
52
-
53
53
setColumnWidth (requestedColumnWidth , recyclerView );
54
54
}
55
55
@@ -78,9 +78,23 @@ public void onDetachedFromWindow(RecyclerView recyclerView, RecyclerView.Recycle
78
78
* @param recyclerView The {@link RecyclerView} to use for determining the number of columns
79
79
*/
80
80
public void setColumnWidth (int gridItemWidth , RecyclerView recyclerView ) {
81
+ requestedColumnWidth = gridItemWidth ;
81
82
setSpanCount (determineColumnCount (gridItemWidth , recyclerView ));
82
83
}
83
84
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
+
84
98
/**
85
99
* Sets the amount of spacing that should be between rows. This value
86
100
* will be overridden when {@link #setMatchRowAndColumnSpacing(boolean)} is set to true
@@ -120,18 +134,29 @@ private int determineColumnCount(int gridItemWidth, RecyclerView recyclerView) {
120
134
return 1 ;
121
135
}
122
136
123
- //If the RecyclerView has been sized then calculate the column width and attach the spacing decoration
137
+ //Calculate the number of columns possible
124
138
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
+ }
126
150
151
+ //Adds or updates the spacing decoration
127
152
if (spacerDecoration != null ) {
128
- spacerDecoration .update (recyclerView .getWidth (), gridItemWidth , count );
153
+ spacerDecoration .update (recyclerView .getWidth (), gridItemWidth , columnCount );
129
154
} else {
130
- spacerDecoration = new SpacerDecoration (recyclerView .getWidth (), gridItemWidth , count );
155
+ spacerDecoration = new SpacerDecoration (recyclerView .getWidth (), gridItemWidth , columnCount );
131
156
recyclerView .addItemDecoration (spacerDecoration );
132
157
}
133
158
134
- return count ;
159
+ return columnCount ;
135
160
}
136
161
137
162
/**
0 commit comments