Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android FixedColumnsHorizontalGridLayoutManager
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