Last active
September 11, 2019 15:03
-
-
Save zakrodionov/ec5e483b76f055506421509dca166ed1 to your computer and use it in GitHub Desktop.
ItemDecoration #view
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 HorizontalPaddingDecoration( | |
private val padding: Float = 2f, | |
private val firstLastPadding: Float = 20f | |
) : RecyclerView.ItemDecoration() { | |
override fun getItemOffsets( | |
outRect: Rect, | |
view: View, | |
parent: RecyclerView, | |
state: RecyclerView.State | |
) { | |
val itemPosition = parent.getChildAdapterPosition(view) | |
if (itemPosition == RecyclerView.NO_POSITION) { | |
return | |
} | |
//outRect.top = padding.dpToPx() | |
//outRect.bottom = padding.dpToPx() | |
when { | |
itemPosition == 0 -> { | |
outRect.left = firstLastPadding.dpToPx() | |
outRect.right = padding.dpToPx() | |
} | |
itemPosition < state.itemCount - 1 -> { | |
outRect.left = padding.dpToPx() | |
outRect.right = padding.dpToPx() | |
} | |
itemPosition == state.itemCount - 1 -> { | |
outRect.left = padding.dpToPx() | |
outRect.right = firstLastPadding.dpToPx() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment