From 1c4215a371fb075bca90858de2c824ab3be7eb39 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Wed, 4 May 2022 02:25:05 -0400 Subject: [PATCH] temporarily comment out the extra methods for grid manager offset the main one needed is the computeVerticalScrollOffset for the app bar anyway --- .../widget/GridLayoutManagerAccurateOffset.kt | 194 ++++++++++-------- 1 file changed, 105 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/GridLayoutManagerAccurateOffset.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/GridLayoutManagerAccurateOffset.kt index e1da74a778..41d80486db 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/GridLayoutManagerAccurateOffset.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/GridLayoutManagerAccurateOffset.kt @@ -4,7 +4,6 @@ import android.content.Context import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import eu.kanade.tachiyomi.R -import kotlin.math.max class GridLayoutManagerAccurateOffset(context: Context?, spanCount: Int) : GridLayoutManager(context, spanCount) { @@ -25,23 +24,23 @@ class GridLayoutManagerAccurateOffset(context: Context?, spanCount: Int) : GridL height } - override fun onLayoutCompleted(state: RecyclerView.State) { - super.onLayoutCompleted(state) - computedRange = null - for (i in 0 until childCount) { - val child = getChildAt(i) ?: return - val position = getPosition(child) - childSizesMap[position] = child.height - childSpanMap[position] = spanSizeLookup.getSpanSize(getPosition(child)) - val type = getItemViewType(child) - childTypeMap[position] = type - if (childTypeHeightMap[type] != null) { - childTypeHeightMap[type]!![position] = child.height - } else { - childTypeHeightMap[type] = hashMapOf(position to child.height) - } - } - } +// override fun onLayoutCompleted(state: RecyclerView.State) { +// super.onLayoutCompleted(state) +// computedRange = null +// for (i in 0 until childCount) { +// val child = getChildAt(i) ?: return +// val position = getPosition(child) +// childSizesMap[position] = child.height +// childSpanMap[position] = spanSizeLookup.getSpanSize(getPosition(child)) +// val type = getItemViewType(child) +// childTypeMap[position] = type +// if (childTypeHeightMap[type] != null) { +// childTypeHeightMap[type]!![position] = child.height +// } else { +// childTypeHeightMap[type] = hashMapOf(position to child.height) +// } +// } +// } override fun onAttachedToWindow(view: RecyclerView?) { super.onAttachedToWindow(view) @@ -53,85 +52,102 @@ class GridLayoutManagerAccurateOffset(context: Context?, spanCount: Int) : GridL rView = null } - override fun computeVerticalScrollRange(state: RecyclerView.State): Int { - if (childCount == 0) return 0 - computedRange?.let { - return it - } - rView ?: return super.computeVerticalScrollRange(state) - var scrolledY = 0 - var spanC = 0 - var maxHeight = 0 - val childAvgHeightMap = HashMap() - for (i in 0 until itemCount) { - val height: Int = getItemHeight(i, childAvgHeightMap) - val spanCurrentSize = childSpanMap[i] ?: spanSizeLookup.getSpanSize(i) - if (spanCount <= spanCurrentSize) { - scrolledY += height - scrolledY += maxHeight - maxHeight = 0 - spanC = 0 - } else if (spanCurrentSize == 1) { - maxHeight = max(maxHeight, height) - spanC++ - if (spanC <= spanCount) { - scrolledY += maxHeight - maxHeight = 0 - spanC = 0 - } - } - } - computedRange = scrolledY - return scrolledY - } - override fun computeVerticalScrollOffset(state: RecyclerView.State): Int { if (childCount == 0) { return 0 } rView ?: return super.computeVerticalScrollOffset(state) - val firstChild = getChildAt(0) ?: return 0 - val firstChildPosition = (0 until childCount) + val firstChild = (0 until childCount) .mapNotNull { getChildAt(it) } - .mapNotNull { pos -> getPosition(pos).takeIf { it != RecyclerView.NO_POSITION } } - .minOrNull() ?: 0 - var scrolledY: Int = -firstChild.y.toInt() - var spanC = 0 - var maxHeight = 0 - val childAvgHeightMap = HashMap() - for (i in 0 until firstChildPosition) { - val height: Int = getItemHeight(i, childAvgHeightMap) - val spanCurrentSize = childSpanMap[i] ?: spanSizeLookup.getSpanSize(i) - if (spanCount <= spanCurrentSize) { - scrolledY += height - scrolledY += maxHeight - maxHeight = 0 - spanC = 0 - } else if (spanCurrentSize == 1) { - maxHeight = max(maxHeight, height) - spanC++ - if (spanC <= spanCount) { - scrolledY += maxHeight - maxHeight = 0 - spanC = 0 - } - } + .mapNotNull { pos -> (pos to getPosition(pos)).takeIf { it.second != RecyclerView.NO_POSITION } } + .minByOrNull { it.second } ?: return 0 + val scrolledY: Int = -firstChild.first.y.toInt() + return if (firstChild.second == 0) { + scrolledY + paddingTop + } else { + super.computeVerticalScrollOffset(state) } - scrolledY += maxHeight - return scrolledY + paddingTop - } - - private fun getItemHeight(pos: Int, childAvgHeightMap: HashMap): Int { - return EstimatedItemHeight.itemOrEstimatedHeight( - pos, - rView?.adapter?.getItemViewType(pos), - childSizesMap, - childTypeMap, - childTypeHeightMap, - childTypeEstimateMap, - childAvgHeightMap, - ) } +// +// override fun computeVerticalScrollRange(state: RecyclerView.State): Int { +// if (childCount == 0) return 0 +// computedRange?.let { +// return it +// } +// rView ?: return super.computeVerticalScrollRange(state) +// var scrolledY = 0 +// var spanC = 0 +// var maxHeight = 0 +// val childAvgHeightMap = HashMap() +// for (i in 0 until itemCount) { +// val height: Int = getItemHeight(i, childAvgHeightMap) +// val spanCurrentSize = childSpanMap[i] ?: spanSizeLookup.getSpanSize(i) +// if (spanCount <= spanCurrentSize) { +// scrolledY += height +// scrolledY += maxHeight +// maxHeight = 0 +// spanC = 0 +// } else if (spanCurrentSize == 1) { +// maxHeight = max(maxHeight, height) +// spanC++ +// if (spanC <= spanCount) { +// scrolledY += maxHeight +// maxHeight = 0 +// spanC = 0 +// } +// } +// } +// computedRange = scrolledY +// return scrolledY +// } +// +// override fun computeVerticalScrollOffset(state: RecyclerView.State): Int { +// if (childCount == 0) { +// return 0 +// } +// rView ?: return super.computeVerticalScrollOffset(state) +// val firstChild = getChildAt(0) ?: return 0 +// val firstChildPosition = (0 until childCount) +// .mapNotNull { getChildAt(it) } +// .mapNotNull { pos -> getPosition(pos).takeIf { it != RecyclerView.NO_POSITION } } +// .minOrNull() ?: 0 +// var scrolledY: Int = -firstChild.y.toInt() +// var spanC = 0 +// var maxHeight = 0 +// val childAvgHeightMap = HashMap() +// for (i in 0 until firstChildPosition) { +// val height: Int = getItemHeight(i, childAvgHeightMap) +// val spanCurrentSize = childSpanMap[i] ?: spanSizeLookup.getSpanSize(i) +// if (spanCount <= spanCurrentSize) { +// scrolledY += height +// scrolledY += maxHeight +// maxHeight = 0 +// spanC = 0 +// } else if (spanCurrentSize == 1) { +// maxHeight = max(maxHeight, height) +// spanC++ +// if (spanC <= spanCount) { +// scrolledY += maxHeight +// maxHeight = 0 +// spanC = 0 +// } +// } +// } +// scrolledY += maxHeight +// return scrolledY + paddingTop +// } +// +// private fun getItemHeight(pos: Int, childAvgHeightMap: HashMap): Int { +// return EstimatedItemHeight.itemOrEstimatedHeight( +// pos, +// rView?.adapter?.getItemViewType(pos), +// childSizesMap, +// childTypeMap, +// childTypeHeightMap, +// childTypeEstimateMap, +// childAvgHeightMap, +// ) +// } override fun findFirstVisibleItemPosition(): Int { return getFirstPos(rView, toolbarHeight)