Forked from janheinrichmerker/SpanningGridLayoutManager.java
Created
February 20, 2020 08:49
-
-
Save vyguera/e6babe1995bff6e8694c2a14b69c595c to your computer and use it in GitHub Desktop.
GridLayoutManager implementation that stretches to fit all grid items on screen and disables scrolling. Useful for dashboards etc.
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
package com.example; | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.GridLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlin.math.ceil | |
import kotlin.math.roundToInt | |
class SpanningGridLayoutManager : GridLayoutManager { | |
private val horizontalSpace: Int | |
get() = width - paddingRight - paddingLeft | |
private val verticalSpace: Int | |
get() = height - paddingBottom - paddingTop | |
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : | |
super(context, attrs, defStyleAttr, defStyleRes) | |
constructor(context: Context, spanCount: Int) : super(context, spanCount) | |
constructor(context: Context, spanCount: Int, orientation: Int, reverseLayout: Boolean) : | |
super(context, spanCount, orientation, reverseLayout) | |
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams { | |
return spanLayoutSize(super.generateDefaultLayoutParams()) | |
} | |
override fun generateLayoutParams(c: Context, attrs: AttributeSet): RecyclerView.LayoutParams { | |
return spanLayoutSize(super.generateLayoutParams(c, attrs)) | |
} | |
override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams { | |
return spanLayoutSize(super.generateLayoutParams(lp)) | |
} | |
override fun checkLayoutParams(lp: RecyclerView.LayoutParams): Boolean { | |
val layoutParams = generateDefaultLayoutParams() | |
return super.checkLayoutParams(lp) && | |
layoutParams.width == lp.width && | |
layoutParams.height == lp.height | |
} | |
private fun spanLayoutSize(layoutParams: RecyclerView.LayoutParams): RecyclerView.LayoutParams { | |
when (orientation) { | |
HORIZONTAL -> layoutParams.width = (horizontalSpace / maxItemsInAllLines()).roundToInt() | |
VERTICAL -> layoutParams.height = (verticalSpace / maxItemsInAllLines()).roundToInt() | |
} | |
return layoutParams | |
} | |
override fun canScrollVertically(): Boolean = false | |
override fun canScrollHorizontally(): Boolean = false | |
private fun maxItemsInAllLines(): Double = ceil(itemCount / spanCount.toDouble()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you ever caught a crash
ArrayIndexOutOfBoundsException: length=0; index=-1
when the size of items change? Like whileRecyclerView
dimensions or span count change.