-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6 feat/홈 : 리스트에 필요한 DividerItemDecoration 추가
- Loading branch information
1 parent
12110af
commit 9392039
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/ftw/hometerview/adapter/DividerItemDecoration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ftw.hometerview.adapter | ||
|
||
import android.content.Context | ||
import android.graphics.Canvas | ||
import android.graphics.Paint | ||
import android.graphics.Rect | ||
import android.view.View | ||
import androidx.annotation.ColorInt | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.view.children | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.ftw.hometerview.R | ||
|
||
class DividerItemDecoration( | ||
context: Context, | ||
@ColorInt private val colorResId: Int = com.ftw.hometerview.design.R.color.gray_200 | ||
) : RecyclerView.ItemDecoration() { | ||
|
||
private val dividerHeight: Int = context.resources.getDimensionPixelSize(R.dimen.dp_size_1) | ||
private val horizontalMargin: Int = context.resources.getDimensionPixelSize(R.dimen.dp_size_14) | ||
|
||
private val paint = Paint().apply { | ||
this.color = ContextCompat.getColor(context, colorResId) | ||
} | ||
|
||
override fun getItemOffsets( | ||
outRect: Rect, | ||
view: View, | ||
parent: RecyclerView, | ||
state: RecyclerView.State | ||
) { | ||
outRect.set(0, 0, 0, dividerHeight) | ||
} | ||
|
||
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { | ||
c.save() | ||
parent.children | ||
.forEach { children -> | ||
c.drawRect( | ||
Rect( | ||
children.left + horizontalMargin, | ||
children.bottom + dividerHeight, | ||
children.right - horizontalMargin, | ||
children.bottom | ||
), | ||
paint | ||
) | ||
} | ||
c.restore() | ||
} | ||
} |