From 26163ce6205d75438afefaddbfdca947c47fec1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gae=CC=88tan=20Muller?= Date: Fri, 15 Jul 2016 22:17:06 +0200 Subject: [PATCH] Improve TableLayoutManager --- .../io/kolumbus/layout/TableLayoutManager.kt | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/kolumbus/src/main/kotlin/io/kolumbus/layout/TableLayoutManager.kt b/kolumbus/src/main/kotlin/io/kolumbus/layout/TableLayoutManager.kt index 221da29..932d535 100644 --- a/kolumbus/src/main/kotlin/io/kolumbus/layout/TableLayoutManager.kt +++ b/kolumbus/src/main/kotlin/io/kolumbus/layout/TableLayoutManager.kt @@ -33,25 +33,20 @@ class TableLayoutManager : LinearLayoutManager { override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) { super.onLayoutChildren(recycler, state) - val firstPosition = 0 - val lastPosition = state?.itemCount ?: 0 - val sizes = mutableMapOf() + val size = Point(0, 0) // Compute the biggest dimension from all the visible items - this.processChild(firstPosition, lastPosition) { index, child -> + this.processChild(0, this.itemCount) { index, child -> child.measure(0, 0) - val size = sizes.getOrPut(index, { Point(0, 0) }) size.x = Math.max(size.x, child.measuredWidth) size.y = Math.max(size.y, child.measuredHeight) } // Set the proper size on all the visible items - this.processChild(firstPosition, lastPosition) { index, child -> - val size = sizes[index] - - child.layoutParams.height = size?.y ?: 0 - child.layoutParams.width = size?.x ?: 0 + this.processChild(0, state?.itemCount ?: 0) { index, child -> + child.layoutParams.height = size.y + child.layoutParams.width = size.x child.post { child.requestLayout() }