Created
January 27, 2021 18:32
-
-
Save hector6872/9955d0c7da008530dfc4a26cecadf418 to your computer and use it in GitHub Desktop.
Android FixedColumnsHorizontalGridLayoutManager
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 FixedColumnsHorizontalGridLayoutManager(context: Context?, spanCountVertical: Int, private var columns: Int, orientation: Int, reverseLayout: Boolean) : | |
GridLayoutManager(context, spanCountVertical, orientation, reverseLayout) { | |
private val safeWidth: Int | |
get() = width - paddingRight - paddingLeft | |
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams = spanLayoutSize(super.generateDefaultLayoutParams()) | |
override fun generateLayoutParams(context: Context, attrs: AttributeSet): RecyclerView.LayoutParams = spanLayoutSize(super.generateLayoutParams(context, attrs)) | |
override fun generateLayoutParams(layoutParams: ViewGroup.LayoutParams): RecyclerView.LayoutParams = spanLayoutSize(super.generateLayoutParams(layoutParams)) | |
private fun spanLayoutSize(layoutParams: RecyclerView.LayoutParams): RecyclerView.LayoutParams { | |
if (orientation == HORIZONTAL) layoutParams.width = | |
round(safeWidth.toDouble() / columns).toInt() else throw IllegalArgumentException("LayoutManager orientation must be GridLayoutManager.HORIZONTAL") | |
return layoutParams | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment