@@ -183,6 +183,24 @@ private class BoxGridMeasureHelper(
183183 ): GridArrangeResult = with (measureScope) {
184184 val placeableCount = measureResult.placeableMeasureInfos.size
185185 val placeablePositionInfos = mutableListOfNulls<PlaceablePositionInfo ?>(placeableCount)
186+ val rowCount = cellHeightConstraintList.size
187+ val columnCount = cellWidthConstraintList.size
188+
189+ val horizontalSpacingPx = horizontalSpacing.roundToPx()
190+ val verticalSpacingPx = verticalSpacing.roundToPx()
191+
192+ var currentX = 0
193+ var currentY = 0
194+ val xPositions = IntArray (columnCount)
195+ val yPositions = IntArray (rowCount)
196+ for (i in 0 until columnCount) {
197+ xPositions[i] = currentX
198+ currentX + = cellWidthConstraintList[i] + horizontalSpacingPx
199+ }
200+ for (i in 0 until rowCount) {
201+ yPositions[i] = currentY
202+ currentY + = cellHeightConstraintList[i] + verticalSpacingPx
203+ }
186204
187205 measureResult.placeableMeasureInfos.fastForEachIndexed { index, placeableMeasureInfo ->
188206 if (placeableMeasureInfo != null ) {
@@ -196,25 +214,12 @@ private class BoxGridMeasureHelper(
196214 height = placeableMeasureInfo.cellConstraints.maxHeight
197215 )
198216
199- val xPosition = if (layoutDirection == LayoutDirection .Ltr ) {
200- val xWithoutSpacing = cellWidthConstraintList.sumOfIndexed { i, c ->
201- if (i < columnPosition) c else 0
202- }
203- xWithoutSpacing + horizontalSpacing.roundToPx() * columnPosition
217+ val xPosition = xPositions[if (layoutDirection == LayoutDirection .Ltr ) {
218+ columnPosition
204219 } else {
205- val layoutWidth = measureResult.layoutSize.width.roundToInt()
206- val xWithoutSpacing = layoutWidth - cellWidthConstraintList.sumOfIndexed { i, c ->
207- if (i <= columnPosition) c else 0
208- }
209- xWithoutSpacing - horizontalSpacing.roundToPx() * columnPosition
210- }
211-
212- val yPosition = run {
213- val yWithoutSpacing = cellHeightConstraintList.sumOfIndexed { i, c ->
214- if (i < rowPosition) c else 0
215- }
216- yWithoutSpacing + verticalSpacing.roundToPx() * rowPosition
217- }
220+ columnCount - columnPosition - 1
221+ }]
222+ val yPosition = yPositions[rowPosition]
218223
219224 val alignedOffset = alignment.align(
220225 size = placeable.size(),
0 commit comments