Skip to content

Commit

Permalink
Merge pull request #19 from Rere-kt/feature/margin-divider-item-decor…
Browse files Browse the repository at this point in the history
…ation

Feature/margin divider item decoration
  • Loading branch information
dionep authored Feb 5, 2021
2 parents 49e4803 + 7e93167 commit 940449b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class MarginDividerItemDecoration constructor(
private val orientation: Int = LinearLayout.VERTICAL,
private val dividerLineMargin: Float = 0f,
@ColorRes private val dividerLineColorRes: Int = android.R.color.black,
private val decorationSize: Int = 0,
private val dividerLineWidth: Float = 0f
private val dividerLineWidth: Float = 0f,
decorationSize: Int = 0
): ItemDecoration() {

private val decorationFullSize: Float = decorationSize + dividerLineWidth

override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
if (orientation == LinearLayout.VERTICAL)
drawVertical(canvas, parent)
Expand All @@ -30,7 +32,7 @@ class MarginDividerItemDecoration constructor(
if (childCount == 0) return
for (i in 0 until childCount - 1) {
val child = parent.getChildAt(i)
val top = (child.bottom + decorationSize / 2).toFloat()
val top = child.bottom + decorationFullSize / 2
val bottom = top
canvas.drawLine(
left + dividerLineMargin,
Expand All @@ -52,7 +54,7 @@ class MarginDividerItemDecoration constructor(
if (childCount == 0) return
for (i in 0 until childCount - 1) {
val child = parent.getChildAt(i)
val left = (child.right + decorationSize / 2).toFloat()
val left = child.right + decorationFullSize / 2
val right = left
canvas.drawLine(
left,
Expand All @@ -75,9 +77,9 @@ class MarginDividerItemDecoration constructor(
) {
if (parent.getChildAdapterPosition(view) != parent.adapter?.itemCount?.minus(1)) {
if (orientation == LinearLayout.VERTICAL)
outRect.bottom = decorationSize
outRect.bottom = decorationFullSize.toInt()
else
outRect.right = decorationSize
outRect.right = decorationFullSize.toInt()
}
}

Expand Down
4 changes: 1 addition & 3 deletions sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class ListFragment: Fragment(R.layout.fragment_main) {
}
itemDecoration(
MarginDividerItemDecoration(
orientation = LinearLayout.VERTICAL,
dividerLineWidth = 2.dip(resources),
decorationSize = 16.dip(resources).int,
dividerLineWidth = 1.dip(resources),
dividerLineMargin = 12.dip(resources),
dividerLineColorRes = R.color.black
)
Expand Down

0 comments on commit 940449b

Please sign in to comment.