Skip to content

Commit b00a9e5

Browse files
committedJan 7, 2016
Updated Documentation
1 parent b59a37f commit b00a9e5

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed
 

‎library/src/main/java/com/devbrackets/android/recyclerext/decoration/SpacerDecoration.java

+40
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ public void update(int horizontalSpace, int verticalSpace) {
6060
this.verticalSpace = verticalSpace;
6161
}
6262

63+
/**
64+
* Sets the edges that are allowed to add spacing. Edges are
65+
* the left side of the left-most items, the right side of the right-most items,
66+
* the top side of the top-most items, and the bottom side of the bottom-most items.
67+
*
68+
* @param edgeSpacingFlags The flags for which edges to add padding to
69+
*/
6370
public void setAllowedEdgeSpacing(@EdgeSpacing int edgeSpacingFlags) {
6471
allowedEdgeSpacing = edgeSpacingFlags;
6572
}
@@ -79,6 +86,14 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle
7986
outRect.bottom = (allowedEdgeSpacing & EDGE_SPACING_BOTTOM) == 0 && isBottomEdge(spanLookup, position, childCount) ? 0 : verticalSpace;
8087
}
8188

89+
/**
90+
* Determines if the item at <code>position</code> is a top-most view
91+
*
92+
* @param spanLookup The SpanLookup related to the parent RecyclerView
93+
* @param position The position to determine if the view is a top-most view
94+
* @param childCount The number of children in the RecyclerView (adapter)
95+
* @return True if the view at <code>position</code> is a top-most view
96+
*/
8297
protected boolean isTopEdge(SpanLookup spanLookup, int position, int childCount) {
8398
int latestCheckedPosition = 0;
8499
for (; latestCheckedPosition < childCount; latestCheckedPosition++) {
@@ -91,11 +106,26 @@ protected boolean isTopEdge(SpanLookup spanLookup, int position, int childCount)
91106
return position <= latestCheckedPosition;
92107
}
93108

109+
/**
110+
* Determines if the item at <code>position</code> is a right-most view
111+
*
112+
* @param spanLookup The SpanLookup related to the parent RecyclerView
113+
* @param position The position to determine if the view is a right-most view
114+
* @return True if the view at <code>position</code> is a right-most view
115+
*/
94116
protected boolean isRightEdge(SpanLookup spanLookup, int position) {
95117
int spanIndex = spanLookup.getIndex(position);
96118
return (spanIndex + spanLookup.getSpanSize(position)) == spanLookup.getSpanCount();
97119
}
98120

121+
/**
122+
* Determines if the item at <code>position</code> is a bottom-most view
123+
*
124+
* @param spanLookup The SpanLookup related to the parent RecyclerView
125+
* @param position The position to determine if the view is a bottom-most view
126+
* @param childCount The number of children in the RecyclerView (adapter)
127+
* @return True if the view at <code>position</code> is a bottom-most view
128+
*/
99129
protected boolean isBottomEdge(SpanLookup spanLookup, int position, int childCount) {
100130
int latestCheckedPosition = childCount -1;
101131
for (; latestCheckedPosition >= 0; latestCheckedPosition--) {
@@ -108,11 +138,21 @@ protected boolean isBottomEdge(SpanLookup spanLookup, int position, int childCou
108138
return position >= latestCheckedPosition;
109139
}
110140

141+
/**
142+
* Determines if the item at <code>position</code> is a left-most view
143+
*
144+
* @param spanLookup The SpanLookup related to the parent RecyclerView
145+
* @param position The position to determine if the view is a left-most view
146+
* @return True if the view at <code>position</code> is a left-most view
147+
*/
111148
protected boolean isLeftEdge(SpanLookup spanLookup, int position) {
112149
int spanIndex = spanLookup.getIndex(position);
113150
return spanIndex == 0;
114151
}
115152

153+
/**
154+
* A helper class to abstract the lookup from different LayoutManagers
155+
*/
116156
protected class SpanLookup {
117157
public WeakReference<GridLayoutManager> gridLayoutManager = new WeakReference<>(null);
118158

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

+32-5
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ protected int determineColumnCount(int gridItemWidth) {
219219
return columnCount;
220220
}
221221

222+
/**
223+
* Calculates and adds the amount of spacing that needs to be between each
224+
* column, row, and the edges of the RecyclerView. This pays attention to
225+
* the value from {@link #setSpacingMethod(SpacingMethod)}
226+
*
227+
* @param recyclerView The RecyclerView to use for determining the amount of space that needs to be added
228+
* @param gridItemWidth The requested width for the items
229+
* @param columnCount The number of columns to display
230+
*/
222231
protected void updateSpacing(RecyclerView recyclerView, int gridItemWidth, int columnCount) {
223232
//Sets the decoration for the calculated spacing
224233
if (spacerDecoration == null) {
@@ -241,12 +250,9 @@ protected void updateSpacing(RecyclerView recyclerView, int gridItemWidth, int c
241250
int freeSpace = usableWidth - (gridItemWidth * columnCount);
242251
int extraSpace = freeSpace - (2 * minColumnSpacingEdge) - (separatorCount * minColumnSpacingSeparator);
243252

253+
//If we can add spacing, then we need to calculate how much and where to add it
244254
if (extraSpace >= spacerCount) {
245-
if (spacingMethod == SpacingMethod.EDGES) {
246-
edgeSpacing = (freeSpace - (separatorSpacing * spacerCount)) / 2;
247-
} else if (spacingMethod == SpacingMethod.SEPARATOR) {
248-
separatorSpacing = spacerCount == 0 ? 0 : freeSpace / spacerCount;
249-
} else {
255+
if (spacingMethod == SpacingMethod.ALL) {
250256
int totalMinEdges = minColumnSpacingEdge * 2;
251257
int totalMinSeparators = separatorCount * minColumnSpacingSeparator;
252258

@@ -257,6 +263,10 @@ protected void updateSpacing(RecyclerView recyclerView, int gridItemWidth, int c
257263

258264
separatorSpacing = spacerCount == 0 ? 0 : totalSeparatorSpace / spacerCount;
259265
edgeSpacing = ((freeSpace - totalSeparatorSpace) / 2) + separatorSpacing;
266+
} else if (spacingMethod == SpacingMethod.EDGES) {
267+
edgeSpacing = (freeSpace - (separatorSpacing * spacerCount)) / 2;
268+
} else { //SEPARATOR
269+
separatorSpacing = spacerCount == 0 ? 0 : freeSpace / spacerCount;
260270
}
261271

262272
edgeSpacing -= separatorSpacing;
@@ -274,6 +284,16 @@ protected void updateSpacing(RecyclerView recyclerView, int gridItemWidth, int c
274284
spacerDecoration.update(separatorSpacing, matchSpacing ? separatorSpacing : rowSpacing / 2);
275285
}
276286

287+
/**
288+
* Performs the actual calculation for determining the number of possible
289+
* columns by using the {@link #maxColumnCount}, {@link #minColumnSpacingEdge}, and
290+
* {@link #minColumnSpacingSeparator} in conjunction with the requested width
291+
* for the items
292+
*
293+
* @param recyclerView The RecyclerView to use when determining the possible number of columns
294+
* @param gridItemWidth The requested width for items to be
295+
* @return The calculated number of possible columns
296+
*/
277297
protected int getColumnCount(RecyclerView recyclerView, int gridItemWidth) {
278298
int padding = recyclerView.getPaddingLeft() + recyclerView.getPaddingRight();
279299
int usableWidth = recyclerView.getWidth() - padding;
@@ -297,6 +317,13 @@ protected int getColumnCount(RecyclerView recyclerView, int gridItemWidth) {
297317
return columnCount;
298318
}
299319

320+
/**
321+
* Removes the padding previously added to the <code>recyclerView</code>
322+
* for the edge spacing. If no padding was previously added, then this
323+
* has no affect
324+
*
325+
* @param recyclerView The RecyclerView to remove the padding from
326+
*/
300327
protected void resetRecyclerPadding(RecyclerView recyclerView) {
301328
recyclerView.setPadding(
302329
recyclerView.getPaddingLeft() - edgeSpacing,

0 commit comments

Comments
 (0)
Please sign in to comment.