Skip to content

Commit

Permalink
Merge pull request #11 from cabriole/dev
Browse files Browse the repository at this point in the history
1.2.0
  • Loading branch information
rubensousa committed Jun 2, 2020
2 parents 2d467a6 + 44bc31c commit a9c925a
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 1.2.0

- Fixed decorations not being applied correctly when the itemCount is 1
- Added `createVertical` and `createHorizontal` helpers to:
- `LinearMarginDecoration`
- `LinearBoundsMarginDecoration`
- `GridBoundsMarginDecoration

# 1.1.0

- Added `GridSpanBoundsMarginDecoration` to apply margins to the bounds of RecyclerViews that use a GridLayoutManager with different span sizes
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Decorator is a library that helps creating composable margins and dividers in Re
## Install

```groovy
implementation 'io.cabriole:decorator:1.1.0'
implementation 'io.cabriole:decorator:1.2.0'
```

Replace x.x.x with the latest version available
Expand Down Expand Up @@ -40,6 +40,12 @@ recyclerView.addItemDecoration(LinearMarginDecoration(
))
```

```kotlin
recyclerView.addItemDecoration(LinearMarginDecoration.createVertical(
verticalMargin = resources.dpToPx(16)
))
```

#### GridMarginDecoration

<img src="examples/gridmargin.png" width=300></img>
Expand Down Expand Up @@ -127,7 +133,10 @@ recyclerView.addItemDecoration(LinearBoundsMarginDecoration.create(

```kotlin
recyclerView.addItemDecoration(GridBoundsMarginDecoration.create(
margin = resources.dpToPx(16),
leftMargin = resources.dpToPx(64),
topMargin = resources.dpToPx(16),
rightMargin = resources.dpToPx(64),
bottomMargin = resources.dpToPx(16),
columnProvider = object : ColumnProvider {
override fun getNumberOfColumns(): Int = 3
}
Expand All @@ -144,21 +153,15 @@ Example for a vertical RecyclerView:
// Add a vertical margin to the first and last items.
// The first item will have a top margin and the last item a bottom margin
recyclerView.addItemDecoration(
LinearBoundsMarginDecoration(
leftMargin = 0,
rightMargin = 0,
topMargin = edgeDecorationSize,
bottomMargin = edgeDecorationSize
LinearBoundsMarginDecoration.createVertical(
verticalMargin = edgeDecorationSize
)
)

// Add a margin between all items
// Add a vertical margin between all items
recyclerView.addItemDecoration(
LinearMarginDecoration(
leftMargin = 0,
rightMargin = 0,
topMargin = marginDecorationSize,
bottomMargin = marginDecorationSize
LinearMarginDecoration.createVertical(
verticalMargin = marginDecorationSize
)
)

Expand Down
2 changes: 1 addition & 1 deletion decorator/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
LIBRARY_VERSION=1.1.0
LIBRARY_VERSION=1.2.0
LIBRARY_GROUP=io.cabriole
LIBRARY_ARTIFACT=decorator
# POM info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,58 @@ class GridBoundsMarginDecoration(

companion object {

/**
* Creates a [GridBoundsMarginDecoration] that applies the same margin
* to the top and bottom sides
*/
@JvmStatic
fun createVertical(
@Px verticalMargin: Int,
gridLayoutManager: GridLayoutManager,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): GridBoundsMarginDecoration {
return GridBoundsMarginDecoration(
leftMargin = 0,
rightMargin = 0,
topMargin = verticalMargin,
bottomMargin = verticalMargin,
columnProvider = object : ColumnProvider {
override fun getNumberOfColumns(): Int = gridLayoutManager.spanCount
},
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

/**
* Creates a [GridBoundsMarginDecoration] that applies the same margin
* to the left and right sides
*/
@JvmStatic
fun createHorizontal(
@Px horizontalMargin: Int,
gridLayoutManager: GridLayoutManager,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): GridBoundsMarginDecoration {
return GridBoundsMarginDecoration(
leftMargin = horizontalMargin,
rightMargin = horizontalMargin,
topMargin = 0,
bottomMargin = 0,
columnProvider = object : ColumnProvider {
override fun getNumberOfColumns(): Int = gridLayoutManager.spanCount
},
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

/**
* Creates a [GridBoundsMarginDecoration] that applies the same margin to all sides
*/
Expand Down Expand Up @@ -175,10 +227,9 @@ class GridBoundsMarginDecoration(
) {
if (columnIndex == 0) {
outRect.left = leftMargin
if (columns == 1) {
outRect.right = rightMargin
}
} else if (columnIndex == columns - 1) {
}

if (columnIndex == columns - 1) {
outRect.right = rightMargin
}

Expand All @@ -188,7 +239,9 @@ class GridBoundsMarginDecoration(
} else {
outRect.bottom = bottomMargin
}
} else if (lineIndex == lines - 1) {
}

if (lineIndex == lines - 1) {
if (!inverted) {
outRect.bottom = bottomMargin
} else {
Expand All @@ -206,10 +259,9 @@ class GridBoundsMarginDecoration(
) {
if (columnIndex == 0) {
outRect.top = topMargin
if (columns == 1) {
outRect.bottom = bottomMargin
}
} else if (columnIndex == columns - 1) {
}

if (columnIndex == columns - 1) {
outRect.bottom = bottomMargin
}

Expand All @@ -219,13 +271,16 @@ class GridBoundsMarginDecoration(
} else {
outRect.right = leftMargin
}
} else if (lineIndex == lines - 1) {
}

if (lineIndex == lines - 1) {
if (!inverted) {
outRect.right = topMargin
} else {
outRect.left = topMargin
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,50 @@ class LinearBoundsMarginDecoration(

companion object {

/**
* Creates a [LinearBoundsMarginDecoration] that applies the same margin
* to the top and bottom sides
*/
@JvmStatic
fun createVertical(
@Px verticalMargin: Int,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): LinearBoundsMarginDecoration {
return LinearBoundsMarginDecoration(
leftMargin = 0,
rightMargin = 0,
topMargin = verticalMargin,
bottomMargin = verticalMargin,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

/**
* Creates a [LinearBoundsMarginDecoration] that applies the same margin
* to the left and right sides
*/
@JvmStatic
fun createHorizontal(
@Px horizontalMargin: Int,
orientation: Int = RecyclerView.HORIZONTAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): LinearBoundsMarginDecoration {
return LinearBoundsMarginDecoration(
leftMargin = horizontalMargin,
rightMargin = horizontalMargin,
topMargin = 0,
bottomMargin = 0,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

/**
* Creates a [LinearBoundsMarginDecoration] that applies the same margin to all sides
*/
Expand Down Expand Up @@ -145,7 +189,8 @@ class LinearBoundsMarginDecoration(
} else {
outRect.bottom = bottomMargin
}
} else if (position == itemCount - 1) {
}
if (position == itemCount - 1) {
if (!inverted) {
outRect.bottom = bottomMargin
} else {
Expand All @@ -164,8 +209,9 @@ class LinearBoundsMarginDecoration(
} else {
outRect.right = rightMargin
}
}

} else if (position == itemCount - 1) {
if (position == itemCount - 1) {
if (!inverted) {
outRect.right = rightMargin
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import androidx.recyclerview.widget.RecyclerView
/**
* An item decoration that applies a margin to all sides of an item
*
* @param leftMargin padding to be applied at the left side of an item
* @param leftMargin margin to be applied at the left side of an item
*
* @param topMargin padding to be applied at the top side of an item
* @param topMargin margin to be applied at the top side of an item
*
* @param rightMargin padding to be applied at the right side of an item
* @param rightMargin margin to be applied at the right side of an item
*
* @param bottomMargin padding to be applied at the bottom side of an item
* @param bottomMargin margin to be applied at the bottom side of an item
*
* @param orientation the orientation of the LayoutManager used by the RecyclerView.
* Default is [RecyclerView.VERTICAL]
Expand All @@ -55,6 +55,50 @@ class LinearMarginDecoration(

companion object {

/**
* Creates a [LinearMarginDecoration] that applies the same margin
* to the top and bottom sides
*/
@JvmStatic
fun createVertical(
@Px verticalMargin: Int,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): LinearMarginDecoration {
return LinearMarginDecoration(
leftMargin = 0,
rightMargin = 0,
topMargin = verticalMargin,
bottomMargin = verticalMargin,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

/**
* Creates a [LinearMarginDecoration] that applies the same margin
* to the left and right sides
*/
@JvmStatic
fun createHorizontal(
@Px horizontalMargin: Int,
orientation: Int = RecyclerView.HORIZONTAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): LinearMarginDecoration {
return LinearMarginDecoration(
leftMargin = horizontalMargin,
rightMargin = horizontalMargin,
topMargin = 0,
bottomMargin = 0,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

/**
* Creates a [LinearMarginDecoration] that applies the same margin to all sides
*/
Expand Down Expand Up @@ -152,10 +196,18 @@ class LinearMarginDecoration(
private fun applyVerticalOffsets(outRect: Rect, position: Int, itemCount: Int) {
if (position == 0) {
if (!inverted) {
outRect.bottom = bottomMargin / 2
if (position == itemCount - 1) {
outRect.bottom = bottomMargin
} else {
outRect.bottom = bottomMargin / 2
}
outRect.top = topMargin
} else {
outRect.top = topMargin / 2
if (position == itemCount - 1) {
outRect.top = topMargin
} else {
outRect.top = topMargin / 2
}
outRect.bottom = bottomMargin
}
} else if (position == itemCount - 1) {
Expand Down Expand Up @@ -183,13 +235,18 @@ class LinearMarginDecoration(
outRect.right = rightMargin
outRect.left = leftMargin / 2
}
} else if (position == itemCount - 1) {
}
if (position == itemCount - 1) {
if (!inverted) {
outRect.left = leftMargin / 2
if (position != 0) {
outRect.left = leftMargin / 2
}
outRect.right = rightMargin
} else {
if (position != 0) {
outRect.right = rightMargin / 2
}
outRect.left = leftMargin
outRect.right = rightMargin / 2
}
} else {
outRect.left = leftMargin / 2
Expand Down

0 comments on commit a9c925a

Please sign in to comment.