Last active
May 25, 2020 12:42
-
-
Save cmorigaki/2a2eacd2724e69e08778d1be0f5c1cbc to your computer and use it in GitHub Desktop.
Margin Item Decoration (GridLayoutManager also)
This file contains hidden or 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
| class MarginItemDecoration( | |
| private val spaceSize: Int, | |
| private val spanCount: Int = 1, | |
| private val orientation: Int = GridLayoutManager.VERTICAL | |
| ) : RecyclerView.ItemDecoration() { | |
| override fun getItemOffsets( | |
| outRect: Rect, view: View, | |
| parent: RecyclerView, state: RecyclerView.State | |
| ) { | |
| with(outRect) { | |
| if (orientation == GridLayoutManager.VERTICAL) { | |
| if (parent.getChildAdapterPosition(view) < spanCount) { | |
| top = spaceSize | |
| } | |
| if (parent.getChildAdapterPosition(view) % spanCount == 0) { | |
| left = spaceSize | |
| } | |
| } else { | |
| if (parent.getChildAdapterPosition(view) < spanCount) { | |
| left = spaceSize | |
| } | |
| if (parent.getChildAdapterPosition(view) % spanCount == 0) { | |
| top = spaceSize | |
| } | |
| } | |
| right = spaceSize | |
| bottom = spaceSize | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment