Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/9955d0c7da008530dfc4a26cecadf418 to your computer and use it in GitHub Desktop.
Save hrules6872/9955d0c7da008530dfc4a26cecadf418 to your computer and use it in GitHub Desktop.
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