Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11594 from t895/autofit-grid
Android: Responsive autofit grid
- Loading branch information
Showing
3 changed files
with
75 additions
and
51 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...ce/Android/app/src/main/java/org/dolphinemu/dolphinemu/layout/AutofitGridLayoutManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
package org.dolphinemu.dolphinemu.layout | ||
|
||
import android.content.Context | ||
import androidx.recyclerview.widget.GridLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.recyclerview.widget.RecyclerView.Recycler | ||
import org.dolphinemu.dolphinemu.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 | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters