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 centerContent: Boolean = false
|
||||||
|
var previousChildCount = 0
|
||||||
|
|
||||||
@SuppressLint("DrawAllocation")
|
@SuppressLint("DrawAllocation")
|
||||||
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
||||||
val availableSize = Pair(right - left, bottom - top)
|
if (childCount == 0 || (!changed && previousChildCount == childCount)) {
|
||||||
if (childCount == 0)
|
super.onLayout(changed, left, top, right, bottom)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
val availableSize = Pair(right - left, bottom - top)
|
||||||
|
previousChildCount = childCount
|
||||||
children.forEach { it.layoutParams = LayoutParams() }
|
children.forEach { it.layoutParams = LayoutParams() }
|
||||||
var cellSize = 0
|
var cellSize = 0
|
||||||
for (index in 1..childCount) {
|
for (index in 1..childCount) {
|
||||||
|
@ -60,11 +64,13 @@ class GridBoxLayout : GridLayout {
|
||||||
cellSize = candidateSize
|
cellSize = candidateSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.onLayout(true, left, top, right, bottom) // Required in this position
|
super.onLayout(changed, left, top, right, bottom)
|
||||||
children.forEach { child ->
|
children.forEach { child ->
|
||||||
child.layoutParams.width = cellSize
|
child.layoutParams.width = cellSize
|
||||||
child.layoutParams.height = cellSize
|
child.layoutParams.height = cellSize
|
||||||
child.requestLayout()
|
child.post {
|
||||||
|
child.requestLayout()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (centerContent) {
|
if (centerContent) {
|
||||||
setPadding((availableSize.first - (columnCount * cellSize)) / 2, (availableSize.second - (rowCount * cellSize)) / 2, (availableSize.first - (columnCount * cellSize)) / 2, (availableSize.second - (rowCount * cellSize)) / 2)
|
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