Last active
August 30, 2023 07:19
-
-
Save bolot/6f1838d29d5b8a87b5fcadbeb53fb6f0 to your computer and use it in GitHub Desktop.
LinearLayoutManager subclass that "peeks", shows a portion of the adjacent child views.
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
class PeekingLinearLayoutManager : LinearLayoutManager { | |
@Suppress("Unused") | |
@JvmOverloads | |
constructor(context: Context?, @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL, reverseLayout: Boolean = false) : super(context, orientation, reverseLayout) | |
@Suppress("Unused") | |
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) | |
override fun generateDefaultLayoutParams() = | |
scaledLayoutParams(super.generateDefaultLayoutParams()) | |
override fun generateLayoutParams(lp: ViewGroup.LayoutParams?) = | |
scaledLayoutParams(super.generateLayoutParams(lp)) | |
override fun generateLayoutParams(c: Context?, attrs: AttributeSet?) = | |
scaledLayoutParams(super.generateLayoutParams(c, attrs)) | |
private fun scaledLayoutParams(layoutParams: RecyclerView.LayoutParams) = | |
layoutParams.apply { | |
when(orientation) { | |
HORIZONTAL -> width = (horizontalSpace * ratio).toInt() | |
VERTICAL -> height = (verticalSpace * ratio).toInt() | |
} | |
} | |
private val horizontalSpace get() = width - paddingStart - paddingEnd | |
private val verticalSpace get() = height - paddingTop - paddingBottom | |
private val ratio = 0.9f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might want to add the following change to avoid showing an empty space when there is a single item: