summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt')
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt
new file mode 100644
index 000000000..be5e4c86c
--- /dev/null
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt
@@ -0,0 +1,61 @@
+// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package org.yuzu.yuzu_emu.layout
+
+import android.content.Context
+import androidx.recyclerview.widget.GridLayoutManager
+import androidx.recyclerview.widget.RecyclerView
+import androidx.recyclerview.widget.RecyclerView.Recycler
+import org.yuzu.yuzu_emu.R
+
+/**
+ * Cut down version of the solution provided here
+ * https://stackoverflow.com/questions/26666143/recyclerview-gridlayoutmanager-how-to-auto-detect-span-count
+ */
+class AutofitGridLayoutManager(
+ context: Context,
+ columnWidth: Int
+) : GridLayoutManager(context, 1) {
+ private var columnWidth = 0
+ private var isColumnWidthChanged = true
+ private var lastWidth = 0
+ private var lastHeight = 0
+
+ init {
+ setColumnWidth(checkedColumnWidth(context, columnWidth))
+ }
+
+ private fun checkedColumnWidth(context: Context, columnWidth: Int): Int {
+ var newColumnWidth = columnWidth
+ if (newColumnWidth <= 0) {
+ newColumnWidth = context.resources.getDimensionPixelSize(R.dimen.spacing_xtralarge)
+ }
+ return newColumnWidth
+ }
+
+ private fun setColumnWidth(newColumnWidth: Int) {
+ if (newColumnWidth > 0 && newColumnWidth != columnWidth) {
+ columnWidth = newColumnWidth
+ isColumnWidthChanged = true
+ }
+ }
+
+ override fun onLayoutChildren(recycler: Recycler, state: RecyclerView.State) {
+ val width = width
+ val height = height
+ if (columnWidth > 0 && width > 0 && height > 0 && (isColumnWidthChanged || lastWidth != width || lastHeight != height)) {
+ val totalSpace: Int = if (orientation == VERTICAL) {
+ width - paddingRight - paddingLeft
+ } else {
+ height - paddingTop - paddingBottom
+ }
+ val spanCount = 1.coerceAtLeast(totalSpace / columnWidth)
+ setSpanCount(spanCount)
+ isColumnWidthChanged = false
+ }
+ lastWidth = width
+ lastHeight = height
+ super.onLayoutChildren(recycler, state)
+ }
+}