Fixes requestLayout() trigerring error message on GridBoxLayout children
This commit is contained in:
parent
7c4c348f8c
commit
c9a9059d68
1 changed files with 10 additions and 4 deletions
|
@ -41,12 +41,16 @@ class GridBoxLayout : GridLayout {
|
|||
)
|
||||
|
||||
var centerContent: Boolean = false
|
||||
var previousChildCount = 0
|
||||
|
||||
@SuppressLint("DrawAllocation")
|
||||
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
||||
val availableSize = Pair(right - left, bottom - top)
|
||||
if (childCount == 0)
|
||||
if (childCount == 0 || (!changed && previousChildCount == childCount)) {
|
||||
super.onLayout(changed, left, top, right, bottom)
|
||||
return
|
||||
}
|
||||
val availableSize = Pair(right - left, bottom - top)
|
||||
previousChildCount = childCount
|
||||
children.forEach { it.layoutParams = LayoutParams() }
|
||||
var cellSize = 0
|
||||
for (index in 1..childCount) {
|
||||
|
@ -60,12 +64,14 @@ class GridBoxLayout : GridLayout {
|
|||
cellSize = candidateSize
|
||||
}
|
||||
}
|
||||
super.onLayout(true, left, top, right, bottom) // Required in this position
|
||||
super.onLayout(changed, left, top, right, bottom)
|
||||
children.forEach { child ->
|
||||
child.layoutParams.width = cellSize
|
||||
child.layoutParams.height = cellSize
|
||||
child.post {
|
||||
child.requestLayout()
|
||||
}
|
||||
}
|
||||
if (centerContent) {
|
||||
setPadding((availableSize.first - (columnCount * cellSize)) / 2, (availableSize.second - (rowCount * cellSize)) / 2, (availableSize.first - (columnCount * cellSize)) / 2, (availableSize.second - (rowCount * cellSize)) / 2)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue