diff --git a/app/src/main/java/emu/skyline/MainActivity.kt b/app/src/main/java/emu/skyline/MainActivity.kt index ac593933..fbf20bf2 100644 --- a/app/src/main/java/emu/skyline/MainActivity.kt +++ b/app/src/main/java/emu/skyline/MainActivity.kt @@ -7,7 +7,6 @@ package emu.skyline import android.content.Intent import android.graphics.Color -import android.graphics.Rect import android.net.Uri import android.os.Bundle import android.provider.DocumentsContract @@ -154,39 +153,13 @@ class MainActivity : AppCompatActivity() { } } - private inner class GridSpacingItemDecoration : RecyclerView.ItemDecoration() { - private val padding = resources.getDimensionPixelSize(R.dimen.grid_padding) - - override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) { - super.getItemOffsets(outRect, view, parent, state) - - val gridLayoutManager = parent.layoutManager as GridLayoutManager - val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams - when (layoutParams.spanIndex) { - 0 -> outRect.left = padding - - gridLayoutManager.spanCount - 1 -> outRect.right = padding - - else -> { - outRect.left = padding / 2 - outRect.right = padding / 2 - } - } - - if (layoutParams.spanSize == gridLayoutManager.spanCount) { - outRect.left = 0 - outRect.right = 0 - } - } - } - private fun setAppListDecoration() { binding.appList.apply { while (itemDecorationCount > 0) removeItemDecorationAt(0) when (layoutType) { LayoutType.List -> Unit - LayoutType.Grid, LayoutType.GridCompact -> addItemDecoration(GridSpacingItemDecoration()) + LayoutType.Grid, LayoutType.GridCompact -> addItemDecoration(GridSpacingItemDecoration(resources.getDimensionPixelSize(R.dimen.grid_padding))) } } } diff --git a/app/src/main/java/emu/skyline/adapter/ItemDecorations.kt b/app/src/main/java/emu/skyline/adapter/ItemDecorations.kt new file mode 100644 index 00000000..0e55db0a --- /dev/null +++ b/app/src/main/java/emu/skyline/adapter/ItemDecorations.kt @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/) + */ + +package emu.skyline.adapter + +import android.graphics.Rect +import android.view.View +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.RecyclerView + +/** + * A [RecyclerView.ItemDecoration] that adds a margin at the bottom of each item + */ +class SpacingItemDecoration(private val padding : Int) : RecyclerView.ItemDecoration() { + override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) { + super.getItemOffsets(outRect, view, parent, state) + outRect.set(0, 0, 0, padding) + } +} + +/** + * A [RecyclerView.ItemDecoration] that adds a margin to the sides of each item in a grid + */ +class GridSpacingItemDecoration(private val padding : Int) : RecyclerView.ItemDecoration() { + override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) { + super.getItemOffsets(outRect, view, parent, state) + + val gridLayoutManager = parent.layoutManager as GridLayoutManager + val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams + when (layoutParams.spanIndex) { + 0 -> outRect.left = padding + + gridLayoutManager.spanCount - 1 -> outRect.right = padding + + else -> { + outRect.left = padding / 2 + outRect.right = padding / 2 + } + } + + if (layoutParams.spanSize == gridLayoutManager.spanCount) { + outRect.left = 0 + outRect.right = 0 + } + } +}