@@ -21,6 +21,7 @@ import android.view.View
21
21
import androidx.annotation.VisibleForTesting
22
22
import androidx.annotation.VisibleForTesting.PRIVATE
23
23
import androidx.recyclerview.widget.GridLayoutManager
24
+ import androidx.recyclerview.widget.LinearLayoutManager
24
25
import androidx.recyclerview.widget.RecyclerView
25
26
26
27
class RecyclerViewItemDecoration (
@@ -34,44 +35,55 @@ class RecyclerViewItemDecoration(
34
35
parent : RecyclerView ,
35
36
state : RecyclerView .State
36
37
) {
37
- setSpacingForDirection(
38
- outRect = outRect,
39
- layoutManager = parent.layoutManager,
40
- position = parent.getChildViewHolder(view).adapterPosition,
41
- itemCount = state.itemCount
42
- )
38
+ when (val layoutManager = parent.layoutManager) {
39
+ is GridLayoutManager -> configSpacingForGridLayoutManager(
40
+ outRect = outRect,
41
+ layoutManager = layoutManager,
42
+ position = parent.getChildViewHolder(view).adapterPosition,
43
+ itemCount = state.itemCount
44
+ )
45
+ is LinearLayoutManager -> configSpacingForLinearLayoutManager(
46
+ outRect = outRect,
47
+ layoutManager = layoutManager,
48
+ position = parent.getChildViewHolder(view).adapterPosition,
49
+ itemCount = state.itemCount
50
+ )
51
+ }
43
52
}
44
53
45
54
// ============================================================================================
46
55
// Private methods
47
56
// ============================================================================================
48
57
49
- private fun setSpacingForDirection (
58
+ private fun configSpacingForGridLayoutManager (
50
59
outRect : Rect ,
51
- layoutManager : RecyclerView . LayoutManager ? ,
60
+ layoutManager : GridLayoutManager ,
52
61
position : Int ,
53
62
itemCount : Int
54
63
) {
55
- outRect.left = spacingPx
56
- outRect.top = spacingPx
64
+ val cols = layoutManager.spanCount
65
+ val rows = if (itemCount % 2 == 0 ) itemCount / cols else itemCount / cols + 1
57
66
58
- when (layoutManager) {
59
- is GridLayoutManager -> {
60
- val cols = layoutManager.spanCount
61
- val rows = if (itemCount % 2 == 0 ) itemCount / cols else (itemCount / cols) + 1
67
+ outRect.top = spacingPx
68
+ outRect.left = spacingPx
69
+ outRect.right = if (position % cols == cols - 1 ) spacingPx else 0
70
+ outRect.bottom = if (position / cols == rows - 1 ) spacingPx else 0
71
+ }
62
72
63
- outRect.right = if (position % cols == cols - 1 ) spacingPx else 0
64
- outRect.bottom = if (position / cols == rows - 1 ) spacingPx else 0
65
- }
66
- is RecyclerView .LayoutManager -> {
67
- if (layoutManager.canScrollHorizontally()) {
68
- outRect.right = if (position == itemCount - 1 ) spacingPx else 0
69
- outRect.bottom = spacingPx
70
- } else if (layoutManager.canScrollVertically()) {
71
- outRect.right = spacingPx
72
- outRect.bottom = if (position == itemCount - 1 ) spacingPx else 0
73
- }
74
- }
73
+ private fun configSpacingForLinearLayoutManager (
74
+ outRect : Rect ,
75
+ layoutManager : LinearLayoutManager ,
76
+ position : Int ,
77
+ itemCount : Int
78
+ ) {
79
+ outRect.top = spacingPx
80
+ outRect.left = spacingPx
81
+ if (layoutManager.canScrollHorizontally()) {
82
+ outRect.right = if (position == itemCount - 1 ) spacingPx else 0
83
+ outRect.bottom = spacingPx
84
+ } else if (layoutManager.canScrollVertically()) {
85
+ outRect.right = spacingPx
86
+ outRect.bottom = if (position == itemCount - 1 ) spacingPx else 0
75
87
}
76
88
}
77
89
}
0 commit comments