committed by
bunnei
3 changed files with 72 additions and 28 deletions
-
61src/android/app/src/main/java/org/yuzu/yuzu_emu/layout/AutofitGridLayoutManager.kt
-
37src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/platform/PlatformGamesFragment.kt
-
2src/android/app/src/main/res/values/dimens.xml
@ -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) |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue