Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- AppBarLayout
- CoordinatorLayout
- CollapsingToolbarLayout
- onLayout
- ViewModel
- 알림
- Navigation
- View
- BOJ
- lifecycle
- DataBinding
- Algorithm
- 코틀린
- CustomView
- Android
- 백준
- room
- Behavior
- recyclerview
- onMeasure
- activity
- sqlite
- hilt
- 알고리즘
- LiveData
- HTTP
- kotlin
- Coroutine
- 안드로이드
- notification
Archives
- Today
- Total
개발일지
Android in A..Z - RecyclerView (ItemDecoration) 본문
ItemDecoration
RecyclerView의 ViewHolder를 꾸미는 역할을 한다. ViewHolder간의 동일한 여백을 주거나, 구분선을 넣기, RecyclerView Background 이미지 주기 등 다양한 효과를 줄 수 있다.
GridSpacingItemDecoration
여백을 주는 ItemDecoration이다.
class GridSpacingItemDecoration(private val spanCount: Int, private val spacing: Int, private val includeEdge: Boolean = true) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
val column = position % spanCount
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount
outRect.right = (column + 1) * spacing / spanCount
outRect.bottom = spacing
if (position < spanCount) {
outRect.top = spacing
}
} else {
outRect.left = column * spacing / spanCount
outRect.right = spacing - (column + 1) * spacing / spanCount
if (position >= spanCount) {
outRect.top = spacing
}
}
}
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDrawOver(c, parent, state)
}
}
- onDraw : ViewHolder가 그려지기 전에 호출된다. => ViewHolder 아래에 보여지게 된다.
- onDrawOver : ViewHolder가 그려진 후에 호출된다. => ViewHolder 위에 보여지게 된다.
- getItemOffsets : ViewHolder에 margin을 주는 함수이다.
DivisionItemDecoration
구분선을 넣는 ItemDecoration이다.
class DivisionItemDecoration(private val colorString: String, private val margin: Int) : RecyclerView.ItemDecoration() {
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)
val paint = Paint().apply {
color = Color.parseColor(colorString)
}
val height = 1.toDp()
for (i in 0 until parent.childCount) {
val child = parent.getChildAt(i)
if (i != parent.childCount - 1) {
c.drawRect(child.left.toFloat() + margin, child.bottom.toFloat() + margin, child.right.toFloat() - margin, child.bottom.toFloat() + height + margin, paint)
}
}
}
}
적용
binding.recyclerView.adapter = adapter.apply { submitList(list) }
binding.recyclerView.addItemDecoration(GridSpacingItemDecoration(1, 10.toDp()))
//binding.recyclerView.addItemDecoration(DivisionItemDecoration("#FFFFFF", 5.toDp()))
Git (예제코드)
github.com/KangTaeJong98/Example/tree/main/Android/RecyclerView
'Android (안드로이드) > RecyclerView' 카테고리의 다른 글
Android in A..Z - RecyclerView (setHasFixedSize) (1) | 2021.01.10 |
---|---|
Android in A..Z - RecyclerView (setHasStableIds) (0) | 2021.01.10 |
Android in A..Z - RecyclerView (AdapterDataObserver) (0) | 2021.01.10 |
Android in A..Z - Recyclerview (ItemTouchHelper) (0) | 2021.01.10 |
Android in A..Z - RecyclerView (Layout Manager) (0) | 2021.01.10 |
Comments